Hi guys,
I have an simple setup: two sensors displaying temperature and humidity (from DH22) and another
sensor with 1 LED and a photocell. All are working fine, the LED turns on/off and I can control the brightness.
The HA is configured to display Temp, humidity and photocell value. For some reason the photocell sensor
does not display the % value, it gives just ‘-’ in the sensor icon. All MQTT messages are sent to HA ok.
Can you help?
sensor 1:
platform: mqtt
state_topic: ‘office/sensor1’
name: ‘Temperature’
unit_of_measurement: ‘°C’
value_template: ‘{{ value_json.temperature }}’
sensor 2:
platform: mqtt
state_topic: ‘office/sensor1’
name: ‘Humidity’
unit_of_measurement: ‘%’
value_template: ‘{{ value_json.humidity }}’
sensor 3:
platform: mqtt
state_topic: ‘office/photocell’
name: ‘Brightness_photocell’
unit_of_measurement: ‘%’
value_template: ‘{{ value_jason.photocell}}’
// function called to publish the photocell brightness
void publishData(int p_analogRead) {
// convert 0-1024 into a percentage
uint8_t photocell = map(p_analogRead, 0, 1024, 0, 100);
// create a JSON object
// doc : https://github.com/bblanchon/ArduinoJson/wiki/API%20Reference
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root[“photocell”] = (String)photocell;
root.prettyPrintTo(Serial);
Serial.println("");
/*
{
“photocell”: “75”
}
*/
char data[200];
root.printTo(data, root.measureLength() + 1);
client.publish(MQTT_SENSOR_TOPIC, data);
}
// read the value of the photocell
uint16_t photocell = analogRead(PHOTOCELL_PIN);
if (photocell < 0 || photocell > 1024) {
Serial.println(“ERROR: Failed to read from the photocell!”);
return;
} else if (photocell != photocell_last){
photocell_last = photocell;
publishData(photocell);
}
delay(1000); //