Report as “good” instead of “unknown” for MQTT binary sensor

Yes I am using off_delay, these sensors have worked for a while (years) but the behaviour has changed in a recent release (2022.2).

That’s fine - I just need to come up with a strategy for handling it. The issue is not that it’s not turning off after triggering in HA, it’s that the state on restart is now unknown. I will manually publish an off state like the example given and it should be ok.

1 Like

The issue here is that a binary_sensor that employs off_delay doesn’t store its state value on the MQTT Broker. When off_delay automatically sets the value to off it’s held exclusively in Home Assistant’s state machine. When Home Assistant starts, it subscribes to the binary_sensor’s MQTT topic and receives … nothing (because there’s no stored value available). In the past, it would report off even though it received nothing (now it reports the value is unknown which is a more accurate assessment of the situation).

One way to ensure the off value is available to Home Assistant on startup is to publish it as a retained message whenever off_delay sets it to off. “Retained message” means the value is stored on the broker and ensures it’s available to Home Assistant when it connects to the broker and subscribes to the binary_sensor’s topic. To achieve this, all that’s required is a simple automation employing a State Trigger and MQTT Publish.

Another way is what DaBIGOne suggested and that’s to publish off to the binary_sensor’s topic exclusively on startup. Basically it’s the same principle (ensure the broker is primed with the desired state value) but only done on startup.

Whichever one you choose to implement, both can be made to support multiple binary_sensors with just a single automation.


EDIT

  alias: Publish off
  trigger:
    - platform: state
      entity_id: binary_sensor.whatever
      to: 'off'
  action:
    - service: mqtt.publish
      data:
        topic: topic/for/whatever
        payload: 'OFF'
        retain: true
1 Like

Happy to report that the overall solution provided by @francisp and the battery state off script/automation provided by @DaBIGOne solved my issue(s). My door/window sensors are now showing their retained state on a HA restart and the batteries are showing a normal (off) state as well.

This is a great community that always has a solution for me :+1:

1 Like

If the binary_sensor reports on, to indicate low battery, what will happen when you restart Home Assistant? The startup automation will set it to off.

How is your low-battery binary_sensor configured to mitigate this?

I agree. Great community!

I am thinking about setting my battery sensor(s) to retain the settings in MQTT (change the false to true in my d below. Then only set the automation to an off state if the sensors are in an unknown state

d = { 'E3E20A':['sensor1','ON','true'],
      'E3E20E':['sensor1','OFF','true'],
      'E3E206':['sensor1b','ON','false'],
      'E3AC0A':['sensor2','ON','true'],
      'E3AC0E':['sensor2','OFF','true'],
      'E3AC06':['sensor2b','ON','false'],
      'F3AA0A':['sensor3','ON','true'],
      'F3AA0E':['sensor3','OFF','true'],
      'F3AA06':['sensor3b','ON','false']
    }

Yup, a bit more to consider than merely forcing everything to off on startup. In addition, don’t forget to take into account what happens when Reload Manually Configured MQTT Entities is executed (unless the entities are configured via MQTT Discovery).

I was doing some searching and found this.

PetricaM

Aug '19

Hi,

I don’t recall exactly but low battery signal gets sent each time the sensor turns into open position (not only when the battery drops below threshold). I have a low battery sensor set in HA and I try to change batteries as soon as it gets triggered but the low battery signal gets sent at least once per day, until there is no sufficient voltage to operate.

So even when the system restarts and is set to off the next time it is open it will also send the low battery signal.

I tried that in one of my sensors and it worked just fine. After home assistant restart the binary sensor showed state off which is what I need.

Can you please help me add additional sensors in the same script ?

Thank you

1 Like