Can any one point / create a tutorial for making mosquito mqtt persistent?
I am looking to query the queue after reboot and I find that sometimes the queue I query has value and sometimes it doesn’t… often it’s an old value that is not the correct one from before the reboot…
When I publish to mqtt I make sure cache is true - but that does not seem to help…
Do I need to configure something more on the mqtt settings on my pi?
can this be done?
or should I opt for an external mqtt for that purpose?
any experience and tips will be greatly appreciated
Your terminology is a little confusing, so I’m not exactly sure what you are trying to do, [quote=“Amir974, post:1, topic:12642”]
for making mosquito mqtt persistent
[/quote]
This would suggest you are running mosquitto broker.
But this would suggest you are running the internal broker. In this link someone found that the retain flag doesn’t seem to be honored by the internal broker, so that may be your problem.
I think his problem is, that mosquitto is sometimes losing the persistent values after reboot. And maybe he’s asking whether he should put mosquitto on another system.
I don’t really know much about mqtt but as far as I understand, the device should send its state again after reconnecting, but for me, this doesn’t work either. After rebooting, my mqtt devices have an ‘off’ state, even as mosquitto seems to be saving the states before reboot.
When I just restart homeassistant and mosquitto stays running, the states are correct.
And yes, with the internal broker the retained states are never honored (in my case… who knows. normally it should…?)
No need to talk about me in the third person I am right here you know…
So - I think I figured it out and it may help you too @sirdir1972
sudo nano /etc/mosquitto/mosquitto.conf
Add the following lines:
autosave_interval 30 autosave_on_changes false
According to this: https://mosquitto.org/man/mosquitto-conf-5.html the default mosquitto setting is to save latest messages every 30 minutes so a local DB file.
The above will tell it to save every 30 seconds…
After that every mqtt sensor that is looking on a particular queue, get’s the latest message as soon as HA restarts (either from a normal restart / reboot / power outage)
(Not sure about the confusion @gpbenton I am running a local mosquitto broker on my HA pi internally )
No offence meant, I was talking to you in the third person because I was replying to @gpbenton and not to you.
I’ll try your changes, I’d have thought it’s not really necessary as the database is saved on exit (I noticed because at first there has been a permission problem and I’ve been getting an error on exit).
do you know if your permission problem was from the Home Assistant db or from the Mosquitto DB ?
So far HA did not support persistent data (state restoration) across sessions (this was introduced today on v 39 which I haven’t tried yet and only apply for input_boolean and input_select)
Oh, the permission problem is nothing much to talk about, I just manually started mosquitto when trying to set things up, so it was running under another user than mosquitto and didn’t have access to write to /var/lib/mosquitto/mosquitto.db. I only mentioned it because it made me aware that mosuitto is actually trying to save states on exit.
True, new version, need to install that right away. I just install appdeamon the save states, will have too see if that interferes now.
I think I got confused after the discussion on internal and external brokers on the other thread.
Saving the state frequently might help if the machine dies unexpectedly, such as a power cut, but as sirdir says, it should happen on a normal shutdown or reboot anyway.
I have one other suggestion, and that is to make a modification to the HA systemd startup script to ensure that HA starts after mosquitto, by adding the After= line in the [Unit] section
$ cat [email protected]
# This is a simple service file for systems with systemd to tun HA as user.
#
# For details please check https://home-assistant.io/getting-started/autostart/
#
[Unit]
Description=Home Assistant for %i
After=network.target mosquitto.service influxdb.service
[Service]
Type=simple
User=%i
ExecStart=/srv/homeassistant/bin/hass
SendSIGKILL=no
Restart=on-failure
[Install]
WantedBy=multi-user.target
Persistent data will work, but not if the state of the switch has changed while HA has been down, so you have to be careful when using it.
I’ve ran in to this issue of non persistent queue in Mosquitto when I was playing with my configuration and did not shut down via the platform but rather by “violently” shutting down the pi and restarting (normal shut down sometimes takes too long for my taste)…
That’s why I like the idea of saving persistent data to DB on much shorter intervals.
I don’t know if this is a good time to play with startup script settings since the issue of preserving state is finally on the table starting with the current version and it seems we can expect more and more components to save state. It remains to be seen how often state save will occur (I might run into the same issue of shutting down and loosing the unsaved states if the interval is too big).
For example, the other day I came back from work to find some problem on HA… Owntracks updated my location to the queue so HA pulled the last state and knew I was home. but than as I was taking care of that problem I disconnected and reconnected the pi and when it came back up - HA pulled an older queue message from before I came back home…
Now that mosquitto is saving the data every 30 seconds, that chances of that happening are much smaller I suppose.