Mqtt topics: repopulate after reboot: not from memory or stored DB?

HI,

I have many mqtt sensors defined, connecting to my Z-wave hub and reading current and total energy consumption, and also reading the data from my smarte meter. All doing well.

Only issue i seem to encounter is that between reboots of HA, all sensors need te repopulate. And data is only collected when pushed (published) on the Hub.

Doesn’t HA read the latest states from memory somehow, or isn’t this data stored in a DB?

The need for repopulating isn’t very elegant, and more importantly, very impractical, when the read data is supposed to be source for further action in automation…

Please help, i might be off, or very close to a solution im not aware of yet :wink:

Cheers,
Marius

It is.

If HA has just started, it doesn’t have it in memory. I think HA could read from the database, but it doesn’t.

You can change your sensors to publish MQTT messages with the retain flag. That will ensure the broker will send the last MQTT message when HA starts up again, and populate the front end that way.

thanks!

ive defined the sensors to read a certain topic from the hub, the hub just sends all events to the Mqtt broker. i don’t think i can change the way the publishing is done, ill ask the dev op the hub though if that would be possible.

its not a matter of defining some kind of setting in the HASSIO for retaining the last value? that would be super practical…

reading from the DB: it’s not done, or is it not possible? Maybe there’s a setting there too?

btw, i have the built-in add-on enabled (maria DB). is that what it writes to? on regular basis i see the db with -shm and -wal extension in the finder. Seems like temporary settings being written to the db?

If the Maria DB is something else, what would be the use for the then?

Cheers,
Marius

just to be precise, I’ve added an example of my mqtt sensors below:

- platform: mqtt
  state_topic: 'mac_address/powerswitch-zwave/85a12df7/usage'
  name: "Vaatwasser Keuken actueel"
  unit_of_measurement: "Watt"

with your remarks on the retain-flag, would it be any good to change the above definition in:

- platform: mqtt
  state_topic: 'mac_address/powerswitch-zwave/85a12df7/usage'
  name: "Vaatwasser Keuken actueel"
  unit_of_measurement: "Watt"
  retain: "true"

im still a bit confused about the mqtt architecture ince in a while, this being a fine example :wink:

Cheers, and thanks for your comments,
Marius

@Mariusthvdb,

The way i get around this issue you have, is to use a custom component as seen here Variable. it allows to store values, and reload even after a full reboot of the system by reading from the DB.

In my setup, I have all my mqtt sensors, storing their values in their respective variables, and I just reload everything when the system starts up.

Hope this helps, regards

sounds exactly like what im looking for. Especially since the hub doesn’t always send events, only when they happen. Which in case of many switches and even smart meter value registers is quite often. Or my solar panels, when rebooting in the evening. Nothing to report, so the Front end says 'Unknown"…

reading from last written memory would solve this. Cool, ill check this out and hope it works!
Thx,
Marius

Just to make clear, the retain flag is set when messages are sent, so setting it on the sensor that receives the message will make no difference.

The persistence package below may help…

But not sensor.

True, but you’d only have to add a couple lines of code to add support for sensors.

HI @Odianosen25,

please let me get back on this, now I’ve settled somewhat in the Mqtt Sensor configuration. Would you care to share an example of your config using the variable?
Thanks,
Marius

You’d think this to be possible too when not configuring HomeAssistant as the publisher, but, as in my settings, a wave hub to which the Homeassistant instance subscribes?

Thanks,
Marius

Yes, if your hub could publish with the retain flag, you wouldn’t have this issue.