Automation or script for managing MQTT Sonoff RF Bridge binary sensor state during hass restart

Hi Everyone,

I am looking for some advice on how best to manage the well known challenge of RF door/window sensors connected to an RF Bridge (Tasmotized & using MQTT in this case). That issue being that the on/off or open/closed state of the MQTT binary sensor being lost during a hass restart.

So far in my experimentation i have arrived at the following configuration to handle this challenge. This is what i have added that’s additional to the mqtt binary_sensors already place.

  • an input_boolean entity for matching each binary_sensor. This is used to save the current door/window state. The state of these entities is saved between a hass restart.
  • 2 automations for setting the input_boolean state when the door/window opens or closes. This is where the current sensor state is saved.
  • another 2 automations that publishs an mqtt payload when hass is starting that is based on the status of the input_boolean. 1 automation published the required ‘door open’ payload if the door was open before the hass instance was restarted and 1 automation that published the required ‘door closed’ payload if the door was closed before the hass instance was restarted.

So far that’s 4 separate automations to deal with just one door or window. This is working so far for 2 doors in my house but considering i have 14 door or window openings that i plan to have fitted with these RF sensors that’s a shit load of automations. Surely there is a better way!

I know there is a better way. It’s just that my current level of templating skill is restricting me from
finding a more elegant solution.

Here is an example of one automation for the mqtt publish part when hass starts…

- id: '1585204760892'
  alias: 'sensor: set back door state OFF at hass startup'
  description: at hass startup, if input_boolean.back_door = OFF then publish mqtt
    payload to force binary_sensor.doorwindow1 to OFF.
  - event: start
    platform: homeassistant
  - condition: template
    value_template: '{{ states(''input_boolean.back_door'') == ''off'' }}'
  - data:
      payload: 2CA00E
      topic: tele/sonoff_rf_bridge1/RESULT
    service: mqtt.publish

I was hoping that i could make this work with just 2 (much larger & more complex) automations and/or scripts in total. 1 automation and/or script for handling the saving of the current door/window state and another for the mqtt publication for making the sensor match the last know state. Is that even a realistic expectation? Is there a more efficient way to handle this? I’m open to suggestions.

Thanks in advance.

Yes there is. Check this topic :

Strategy 2 is what you need.


Wow Francis, that’s amazing. I’m so thrilled that you understood the issue exactly. If only i could have searched the forum a little better to find your original post.
Thanks so much for taking the time to share.

Thanks again Francis. Your solution is working perfectly.