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);
}