Binary sensor state after restart

I have just set up some door and window sensors but every time I restart HA they revert to ‘Closed’. I understand that this is expected behaviour i.e. their state is not saved in the same way as input_booleans for example.

Is there a way around this other than the horrible kludge of saving the binary sensor state into an input boolean every time it changes?

Why would they be changing to Closed if the door/window isn’t closed? If the sensor hasn’t updated yet, it should be ‘unknown’ after restart, not ‘Closed’. Maybe you could share your sensor code.

Thanks for looking.
The sensors are all 433MHz connected to a Tasmotized Sonoff RF Bridge.

  # Door - SensorID 01
  - platform: mqtt
    name: "Door1"
    state_topic: "tele/sonoff_rf01/RESULT"
    value_template: '{{value_json.RfReceived.Data}}'
    payload_on: "AC860A"
    payload_off: "AC860B"
    device_class: Door
    qos: 1
    retain: true

and the actual MQTT message is in the form


Interesting. Well, I don’t use MQTT (you should probably use that tag when creating a topic that involves MQTT), and I haven’t looked at the code, but I would think that the binary_sensor’s state would be ‘unknown’ until it receives the first message from the broker. Are you sure the state is ‘off’, or is it just not ‘on’?

Well, all I can say is that when HA restarts my binary_sensors which have a type of ‘door’ or ‘window’ all show as ‘Closed’ irrespective of their state at that time.

Now that this seems to be ‘surprising’ behaviour I will create a new thread with the MQTT tag.

Thanks for your input.

On the original point, even when sensors show as ‘Unavailable’ do you have a way that you reset them to their original state at system start which doesn’t involve a ‘kludge’?

Did you find any solution? I have similar problem.

Look at this topic :

and use the retain flag

1 Like