I have a few Door / Window sensors that are RF connecting to an SonoffRF Bridge.
They work fine, but if I restart HA the status is lost. They go back to closed state regardless of if the door was closed or not prior to restarting HA.
Is there a way to get the binary sensor to retain it’s state during a restart?
Or do I need to have MQTT set an input_boolean and then use a binary sensor off that input_boolean?
I believe you have to be able to configure retain=true in the sensor(s). That way the MQTT broker retains the last message and replays it to HA after the restart. Is there a web interface for the sending device where you can configure that?
The device sending the MQTT message to the MQTT broker needs to set the retain flag, looks like this is your SonoffRF Bridge. HA is on the receiving end…
I’m still fairly new to MQTT but this is my understanding of it.
I have had the same problem with the Sonoff basic with tasmota. I used the “PowerRetain 1” command, I don’t know how the bridge works with the commands. If you are using tasmota firmware on the bridge you can look here.
Setting retain true in HA will not do what you want. If it was a switch in HA that would retain the MQTT topic when it is turned on/off in HA.
You need it the other way around. The device publishing the MQTT message to send the retain flag. In this case the Sonoff Bridge. That way when HA restarts and checks the MQTT topics the message is still there for it to read.
I know sorry I hit the wrong reply the tried editing it but it wont let me change who I replied to. I tried deleting the post and recreating it but was told the post was the same as a previous reply
I assume the RF Bridge publishes to an MQTT Broker. What kind of firmware is it using?
If it is Tasmota, I believe you need to set SensorRetain 1 to make it publish with retain=true. This instructs the MQTT Broker to retain a copy of messages published to it by the RF Bridge. As a result, subscribers connecting to the MQTT Broker (like Home Assistant after a restart) will get the current state of sensors handled by the RF Bridge.
There is just one topic, tele/sonoffBridge/RESULT, representing the states of multiple sensors.
You use the contents of the payload to differentiate between the sensors. Effectively, the payload is used like a fingerprint to identify the sensor.
If all of the above is true then I don’t believe you can use retain to have the MQTT Broker store the state of each sensor. All sensors are represented by only one topic and only that single topic’s messages can be retained. In practice this means (at best) that at the moment Home Assistant reconnects to the Broker, it only has the state of the last sensor to have published something.
You’ll need another means to store the sensor’s state. I believe the value of an input_boolean survives a restart. It can be used (via automations) to store the binary_sensor’s state-changes and restore its state upon startup. It’s not pretty but that’s the current state of things (no pun intended).
Etc and just using the “Data”
It doesn’t even retain the last value sent.
Not to worry I will setup some input_boolean’s
It’s also a shame the Door Class isn’t tri state instead of being just on / off as these sensors have a tamper code as well.
I will look into perhaps setting up a cover. Or I may just have a separate tamper sensor?
FWIW, my sensors publish on separate topics and use retain=true so their messages are retained by the Broker. Upon startup, HA gets the current state of all sensors (when it re-subscribes to all sensor topics). However, in your situation (with a single topic), it’s no longer worthwhile to explore why the last published message isn’t being retained.
Or I may just have a separate tamper sensor?
That’s what I’d do. For example, the climate component does not support the fan’s current state so I handle it using an MQTT Sensor.
- platform: mqtt
name: "HVAC Fan Status"
icon: mdi:fan
state_topic: "premise/home/thermostat/fanstatus"
value_template: >-
{% set values = { '0':'Off', '1':'On' } %}
{{ values[value] if value in values.keys() else '?' }}
I’ll throw out this idea as an alternative. I would continue to use a binary_sensor to represent the device’s state. It provides the best representation via its support using device_class.
The input_boolean would only serve to store the sensor’s state. In fact, it would be concealed from the UI using its hidden parameter. An automation, triggered upon Home Assistant’s startup, would use the input_boolean’s state to set the corresponding binary_sensor’s state.
There are other ideas in the following thread for storing values in some form of ‘global variable’ that can survive a restart.
That solution is an enormous kludge. Nevertheless, given the available options, it’s a good kludge.
In nutshell, it’s splitting a single topic (representing many devices) into multiple topics (one per device). It allows you to subscribe to each unique sensor.
HA subscribes to the RF Bridge’s single topic, parses the received message into separate topics and publishes each one (with retain=true). Then it subscribes to each one of the topics it just published (where each topic now represents a unique sensor).
FWIW, if you are already running Node-Red, you can create a flow to do the same thing. It would subscribed to the RF Bridge, parse the incoming messages, and publish each sensor’s message in a separate topic (with retain=true). Now your binary_sensors can subscribe to unique topics whose messages are available when HA restarts. Nice.