Mqtt value after restart ha

Dear friends, I have a little problem, I have a classic mqtt sensor for rain gauge. Everything works well but when I restart HA in the graph show always the same value peak… The gost value. Do you know why?
P.S. The peristance in mqtt setting is disabled. FB_IMG_1535978844712

I assume from this you mean the retain flag. It may be disabled now, but if a message was sent in the past, the broker will still keep the message in its database, and send that message whenever HA connects (such as after a restart), which sounds like what is happening.

To clear the message from the broker’s database, you need to send it a message with that topic, the retain flag set and a null payload. You can do it with mosquitto_pub like this

mosquitto_pub -t "Your Topic" -r -n 

You may need to add host (-h), password (-P), and user (-u) parameters to this, depending on your set up.

You can do the same thing from the send mqtt page of the HA developer tools, but I can’t remember the json exactly.

1 Like

I assume from this you mean the retain flag.

No I mean the persistance in mosquitto.conf

> persistence false

By the way, thanks for the answer I try also cleat the retain flag with your procedure but nothig.
After that I found where is the problem…
I use this script for mantain a persistance of some state in HA (GitHub - gazoscalvertos/HASS-Persistence: Home Assistant Persistence HA HASS Automation) and I had added this sensor for test time ago.
I leater removed them, but in one file (persistance.json) they remained stored.
I removed them from persistance.json file and everything is OK now.

1 Like

Could you have a mix of retained and non-retained messages? So when HA starts it gets the last retained message, which may not be the last message. You could install MQTT.fx and subscribe to the messages to see what’s coming through and whether or not the retain flag is set.

Thanks it was no retail think. As you can read it was a persistance component in HA.

same here, also in “status” sensors i have ghost value… in entities that are “off” i see a little “on” corresponding to hass restarts. I can say that is not mqtt retain related after that tries:

  • retain non specified in messages
  • enabled retain
  • disabled retain
  • restart of mosquitto after hass startup
  • disabled mosquitto
    In any above cases the ghost value shows up

Did you read my previous post about removing retained messages?

yes, and already known. Also restarting mosquitto will delete all retained messages. but that’s not the point: if mosquitto is disabled (or uninstalled), where hass gets initial (wrong) value? Probably the problem is in hass persistence or other components.
Here some notes:

  • wrong data (at least on state sensors) does not affect automation (automation where not triggered by that ghost value)

  • when i’ve seen that for the first time my thing was that the “on” states comes from an hardware failure/error on bootup/first message. Before knowing that automations were not affected, i’ve made a second entity that comes “on” only if the first is “on” for 2 minutes… Result? Both have ghost value on restart… here the conf:

    - platform: mqtt
      name: 'Crepuscolare Direct'
      state_topic: '192_168_1_13/D2_5'
      payload_on: "1"
      payload_off: "0"
    
    - platform: template
      sensors:
        crepuscolare_debug:
           friendly_name: "Crepuscolare"
           delay_off:
             minutes: 2
           delay_on:
             minutes: 2
           value_template: "{{ is_state('binary_sensor.crepuscolare_direct', 'on') }}"

I realise it has nothing to do with your problem, but in case someone gets the wrong idea, restarting mosquitto does not delete its database, so retained messages are not cleared by doing this.

@mr-varga

Hi

I’m having exactly the same problem as in your initial post.
Have you ever found a solution for this ?

Yes in my case the problem was the persistance.

If you are using Tasmota there are SetOption Commands you can issue to publish to the broker as a persistent message then when HA reconnects to the broker it gets the value immediately.