Persistent statistics sensors

I am running several statistics sensors to calculate hourly and daily energy consumption. These sensors are fed to MQTT sensors every hour (or 24 hours). The problem is that as soon as I reboot they are set to zero again.

Is there any way through which I can make them persistent? It is a bit cumbersome to miss a complete day of energy readings as soon as I reboot…

When you publish to the the mqtt topics set the retain flag to true.

Also are you using the max_age config parameter in your statistics sensors? Because I found it did not work as intended.

https://github.com/home-assistant/home-assistant/issues/16595

Yep: retain is set to true. I reset the mqtt sensors through an automation after reading them and that works just fine. So as an example for the 1 hour readings I:

  1. Publish the mean value to mqtt on the whole hour
  2. Reset the statistics sensor to zero

One way to do this would be to use Influx as your backend database:

and then use an InfluxDB sensor to bring values back into HA:

Very interesting as I am a longtime InfluxDB user. But how can I make this happen? All sensors/actuators record their state to the InfluxDB, but how can I get only the data after a restart into the sensor (and only after a restart)?

Personally, I would use the telegraf MQTT input to store all the data in influxdb, and then use the influxdb sensor to read the data I needed in HA. HA isn’t really a reliable long term database - too many things go wrong that require it to be deleted.

Also for this solution: how would the automation work?

I’m not sure which automation you are referring to, but you could trigger an automation off the influxdb sensor the same as you could off any other sensor.

@gpbenton I understand, but after a reboot should I trigger some kind of automation that says “If reboot < 1 min, then write last value of sensor from MQTT back to the sensor value”?

If you are using the mosquitto broker you can edit the mosquitto.conf file to save the latest messages to a persistent database that should survive a reboot. See here for more:

I was actually suggesting not having any sensors set by the MQTT messages (driectly), but by reading the data from the influxdb database.

The influxdb sensors are set by the component reading the influxdb database automatically on start up, so there would be no need for an automation to do that.

BTW, if you don’t want to use influxdb, I think using the retain flag should work, providing you are using a broker that supports it. But if you want persistent data and you already use influxdb, it seems sensible to use that rather than HA, which has a rather unreliable database.

Aha, now I understand! In essence you ONLY use the influxdb/MQTT sensors and not the sensors as they appear in Homassistant (ie sensor.wemo_insight) but instead directly feed them to the Influxdb and show these values in Homeassistant and use the Influxdb sensors for automation etc.

That would mean a massive overhaul of my setup as I currently only use Influxdb to store values and not as a sensor…