Hi all, so far I’ve successfully been able to use MQTT Discovery to discover some custom sensors I’ve created. One battery optimization I’m trying to do for my custom sensor is to cache multiple readings over time, each with its own timestamp, and then periodically publish all of the cached readings via MQTT (this allows for less time with the wifi radio enabled, reducing power consumption).
What I’m trying to figure out is how to update my MQTT Discovery configuration to include a timestamp as part of the sensor data payload so that Home Assistant can use that timestamp for plotting the data instead of the publish timestamp.
My first thought was to potentially add a timestamp attribute to each Sensor, does anyone know if that will work? If so, I’d love some input on how to write the json_attr_tpl
template as I’m having a hard time figuring out how to form it.
My first attempt at adding an attribute to the config topic hasn’t worked:
Config topic:
homeassistant/sensor/Magtag-c7fd1a54a342_Magtag_CO2_SCD41_SCD41_CO2/config
{
"device_class": "carbon_dioxide",
"stat_t": "~/state",
"obj_id": "Magtag_CO2_SCD41_SCD41_CO2",
"~": "homeassistant/sensor/Magtag-c7fd1a54a342",
"unit_of_meas": "ppm",
"uniq_id": "Magtag-c7fd1a54a342_Magtag_CO2_SCD41_SCD41_CO2",
"stat_cla": "measurement",
"dev": {
"mdl": "Adafruit MagTag with ESP32S2",
"ids": "c7fd1a54a342",
"name": "Magtag_CO2_SCD41",
"sw": "7.3.3 on 2022-08-29",
"mf": "Espressif"
},
"name": "SCD41 CO2",
"json_attr_t": "~/state",
"val_tpl": "{{ value_json.SCD41_CO2 | round(0) }}",
"json_attr_tpl": "{{ value_json.Timestamp | timestamp_local }}"
}
The State topic payload looks like this:
{
"SCD41_Humidity": 36.0413,
"Batt_Voltage": 4.08397,
"SCD41_Temperature": 22.7425,
"Timestamp": 1672327760,
"SCD41_CO2": 579
}