Need to enable RETAIN: TRUE on 1 MQTT sensor

Hi :slight_smile:

I have a TH10 who is managing many temperature sensors (1-wire) and who send values each minute to Home Assistant via MQTT (Mosquito). This is working well since 3 years.

I have a device (Rasp Pi 5 who manage a Skycam) who needs to pick up 1 value from MQTT (outside temp). Everything is set well on this side, I checked.

But the value is at 0, null.

I searched and found that on MQTT, the value, between each new value (from my TH10) is null / 0.
And so, I need to enable the RETAIN option at TRUE.

I tried, in the configuration.yaml file, like this:

        - name: "Temperature"
          unique_id: "73948473"
          state_topic: "th10/outside"
          retain: "true"
          device_class: "temperature"
          unit_of_measurement: "°C"
          state_class: "measurement"

I saved then restarted HA, then… the sensor becomes “unavailable”, not any value is displayed in HA.

Please, do you know how to activate RETAIN just for 1 sensors? I don’t find any working information.

Thanks :slight_smile:

The retain flag has to be set by the device sending the message to the mqtt broker.

Ah !
Thanks a lot. We will look how to do it on the TH10 :slight_smile:

That won’t help though if it is sending null / 0 values.

Try this:

        - name: "Temperature"
          unique_id: "73948473"
          state_topic: "th10/outside"
          value_template: "{{ value if value is defined and value !=0 else this.state }}"
          device_class: "temperature"
          unit_of_measurement: "°C"
          state_class: "measurement"

The TH10 is not sending 0 / null, it’s always sending a good value.

But MQTT dones’t keep value between 2 values sent from the TH10, so my other device sees 0/null, it’s why I need to activate RETAIN, to keep the value in “memory” on HA, like that mon other device can catch/receive the value.

Then retain should work.

I tried this, the sensor is still valid, but mon other device still sees 0.

I will try to change the RETAIN option on the TH10, then I will come back here to explain.

Thanks :slight_smile:

Hello JeyC,

This can set a retain also.
Troubleshooting MQTT? MQTT-Explorer can help you be successful!
If you have Add-ons available, try adding this Add-on repository to your Add-on list. GitHub - GollumDom/addon-repository.
With this you can see that the broker sees and likely find the bug.