MQTT Binary Sensor initial state

The binary sensor state will be updated only after a new message is published on state_topic matching payload_on or payload_off . If these messages are published with the retain flag set, the binary sensor will receive an instant state update after subscription and Home Assistant will display the correct state on startup. Otherwise, the initial state displayed in Home Assistant will be unknown .
[source]

Is this definitely true? Mine seem to take on a locked or closed state on reboot (locked if a lock and closed if a door etc)

1 Like

Yes, it’s true.

What you’re observing is the fact the binary_sensor defaults to off. For example, given this definition:

  - platform: mqtt
    name: 'Bathroom Door'
    state_topic: 'home/sensor1'
    device_class: Door

it appears like this in the UI:

Screenshot%20from%202019-05-24%2016-39-42

It indicates ‘Closed’ which corresponds to state = off. Yet the topic home/sensor1 doesn’t even exist on my MQTT broker, let alone have a value.

Some would argue that defaulting to off in the absence of any received data is misleading. The solution would be to define an availability topic. In the absence of data, the binary_sensor’s state will be unavailable.

If I add this line to the previous definition:

    availability_topic: 'home/sensor1/status'

the UI will display this:

Screenshot%20from%202019-05-24%2016-52-48