V_UNIT_PREFIX not working/recognised for Air Quality sensor CSS811



  • Hey there!

    Last weekend I got my CCS811 sensor working to measure the eCO2 and TVOC lvls. It reports the values just fine and the sensor is recognised by HA. Upon creating a UI where the values can be seen in a graph, I am getting the HA error that the sensors has no unit of measurement.

    Which was true so I added the V_UNIT_PREFIX to the message, unfortunately this did not solved the error. I am not sure whether the prefix is not send correctly or that HA just can't deal with it. I presume the former and looking for any advice to ensure that it is sent correctly to the HA/controller and getting insight in my mistake. I have read about solving the issue within HA but I'd rather fix it at the core in the sketch.

    Below the code, everything works fine. Hope the community can help out!

    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached 
    #define MY_RADIO_RF24
    
    
    // Include libraries
    #include <MySensors.h>
    #include "Adafruit_CCS811.h"
    #include <SI7021.h>
    
    
    static Adafruit_CCS811 ccs;
    #define CHILD_ID_eCO2  0
    #define CHILD_ID_TVOC 1
    
    
    // Sleep time between sensor updates (in milliseconds)
    static const uint64_t UPDATE_INTERVAL = 10000;
    
    void presentation()  
    { 
      // Send the sketch info to the gateway
      sendSketchInfo("eCO2, TVOC", "0.02");
    
      // Present sensors as children to gateway
      present(CHILD_ID_eCO2, S_AIR_QUALITY, "eCO2");
      present(CHILD_ID_TVOC, S_AIR_QUALITY, "TVOC");
    
    
    }
    
    void setup() 
    { 
      while (not ccs.begin())
      {
        Serial.println(F("Sensor not detected!"));
        delay(5000);
      }
      Serial.println("CCS811 test");
    
     
    }
    
    void loop() {
      if(ccs.available()){
        if(!ccs.readData()){
        // Read temperature & humidity from sensor.
        const float eCO2 = float(ccs.geteCO2());
        const float TVOC = float(ccs.getTVOC());
        
        Serial.print("CO2: ");
        Serial.print(eCO2,0);
        Serial.print("ppm, TVOC: ");
        Serial.print(TVOC,0);
        Serial.println("ppb");
    
        static MyMessage msgeCO2(CHILD_ID_eCO2,  V_LEVEL);
        static MyMessage msgeCO2Prefix(CHILD_ID_eCO2, V_UNIT_PREFIX);
        static MyMessage msgTVOC(CHILD_ID_TVOC, V_LEVEL);  
        static MyMessage msgTVOCPrefix(CHILD_ID_TVOC, V_UNIT_PREFIX);     
    
        send(msgeCO2.set(eCO2, 0)); //1 decimal
        send(msgeCO2Prefix.set("ppm"));
        send(msgTVOC.set(TVOC, 0));
        send(msgTVOCPrefix.set("ppb"));
            
        }
        else{
          Serial.println("ERROR!");
          while(1);
        }
      }
    
      // Sleep until next update to save energy
      sleep(UPDATE_INTERVAL); 
    }
    


  • Hi!

    Had a similar problem with a pressure sensor and in the end it turned out that I had not defined the version in my home assistant configuration file. Like this:

    mysensors:
    gateways:
    - device: '/dev/ttyACM1'
    persistence_file: 'mysensors1.json'
    version: '2.3'

    Without the line saying "version: '2.3" the V_UNIT_PREFIX was not recognized.



  • @niclas interesting, was not aware of that.
    I had a quick look at my configuration file and did see something strange, within the mysensors gateways code two devices were listed. The second one is unknown to me and was misplaced in the code (indents etc.). So I fixed that. All mysensors nodes are saved in mysensors.json. I have no idea what /dev/ttyACM1 is doing here, neither is mysensors1.json anywhere to be found.
    As a gateway I use a RPI, as you can see below the version is mentioned. Rebooting the HA after ordering the configuration had no visible effect.

    mysensors:
      gateways: 
        - device: '192.168.178.26'
          persistence_file: 'mysensors.json'
          tcp_port: 5003
        - device: '/dev/ttyACM1'
          persistence_file: 'mysensors1.json'
      persistence: true
      version: '2.3'
    
    


  • @Sebex
    Hmm... Too bad. Then I guess something could be wrong with your code.

    This is my post: https://forum.mysensors.org/topic/10976/unit-of-measurement-for-pressure-sensor
    Perhaps the other solution setting the unit in home assistant could work?

    Here is my temperature/pressure sensor code, I couldn't find anything wrong with your code but perhaps you could find something.

    #define CHILD_ID_TEMP 0
    #define CHILD_ID_PA 1
    
    #define I2C_ADDRESS 0x77
    BMP180I2C bmp180(I2C_ADDRESS);
    
    
    
    MyMessage msg_temp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msg_pa(CHILD_ID_PA, V_PRESSURE);
    MyMessage msgPrefix(CHILD_ID_PA, V_UNIT_PREFIX);  // Custom unit message.
    
    
    float temperature = 4;
    float pressure = 4;
    
    void presentation()
    {
      Wire.begin();
        //begin() initializes the interface, checks the sensor ID and reads the calibration parameters.  
      if (!bmp180.begin())
      {
        Serial.println("begin() failed. check your BMP180 Interface and I2C Address.");
        while (1);
      }
      //reset sensor to default parameters.
      bmp180.resetToDefaults();
    
      //enable ultra high resolution mode for pressure measurements
      bmp180.setSamplingMode(BMP180MI::MODE_UHR);
      
      sendSketchInfo("Temp_pa sensor", "1.1");
      present(CHILD_ID_TEMP, S_TEMP, "temp1");
      present(CHILD_ID_PA, S_BARO, "pres1");
    
    }
    
    bool once = false;
    
    void loop()
    {
      //start a temperature measurement
      if (!bmp180.measureTemperature())
      {
        Serial.println("could not start temperature measurement, is a measurement already running?");
        return;
      }
    
      //wait for the measurement to finish. proceed as soon as hasValue() returned true.
      do
      {
        delay(100);
      } while (!bmp180.hasValue());
    
      temperature = bmp180.getTemperature();
      send(msg_temp.set(temperature, 1));
    
      if (!bmp180.measurePressure())
      {
        Serial.println("could not start perssure measurement, is a measurement already running?");
        return;
      }
    
      //wait for the measurement to finish. proceed as soon as hasValue() returned true. 
      do
      {
        delay(100);
      } while (!bmp180.hasValue());
      
      pressure = bmp180.getPressure() / 100.0;
    
      if(!once){
      send(msgPrefix.set("mbar"));  // Set custom unit.
      once = true;
      }
      send(msg_pa.set(pressure, 1));
    
    
      Serial.println(temperature);
      Serial.println(pressure);
    
      sleep(10000);
    
    }
    


  • @niclas hey man it seems to be working due to putting the version in the config file, at the right indent. So your suggestion worked after all, guess I had to wait a bit before it was recognised by HA. Thnx, problem solved 😃



  • @Sebex How is V_PRESSURE being shown in Home Assistant. For me it is showing as a Text instead of a number. Is it the same for you?
    26abdcbe-6bbb-49d5-9676-4d22c8646b7f-image.png



  • @Puneit-Thukral The MySensors integration in Home Assistant has no unit associated with V_PRESSURE, as it can have a variety of units (pascal, bar, mercury, psi,...). You either need to specify a custom unit_of_measurement in HA, like so:

    sensor.pir_bme_sensor_143_4:
      friendly_name: Air Pressure
      unit_of_measurement: hPa
    

    https://www.home-assistant.io/docs/configuration/customizing-devices/

    ... or present a custom unit in MySensors with V_UNIT_PREFIX, like so:

    #define CHILD_ID_PRESSURE 4 // <- use the same ID as for the sensor
    MyMessage msgPrefix(CHILD_ID_PRESSURE, V_UNIT_PREFIX);
    void setup() {
      send(msgPrefix.set("hPa"));
    }
    

    https://www.home-assistant.io/integrations/sensor.mysensors/#custom-unit-of-measurement



  • @Puneit-Thukral in your screen it shows a number right?

    edit: is there a method to resize uploaded pictures here?

    alt text



  • Hi @Sebex , I followed @BearWithBeard advise (thank you) and customised the entity in home assistant. And now it shows as numbers. 39b5668a-ba4f-4bca-b6b0-1115e7f7eb11-image.png


Log in to reply
 

Suggested Topics

  • 3
  • 3
  • 24
  • 1
  • 2
  • 15

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts