Two-Code Door Sensor

Ok, as I had the code I would open the switch and restart HA or even reboot and when it came back up the binary_sensor was still showing open.

I changed my binary_sensor to match yours:

- platform: mqtt
  name: "TestDoorSwitch State"
  state_topic: "doorswitches/testdoorswitch"
  device_class: door

And I changed my automation down to a single one to match yours as well:

- id: testdoorswitchopenedclosed
  alias: testdoorswitch-openedclosed
  description: TestDoorSwitch Action
  trigger:
  - platform: mqtt
    topic: rflink/SelectPlus-065afa
  - platform: mqtt
    topic: rflink/SelectPlus-065af8
  action:
    service: mqtt.publish
    data_template:
      topic: doorswitches/testdoorswitch
      payload: "{{ 'ON' if trigger.topic[-1] == 'a' else 'OFF' }}"
      retain: true      

It works and it keeps the last setting after restart or reboot.

However, if HA is down and I change the state of the switch it is not picked up when HA comes back online.

I think that makes sense since RFLink & espRFLinkMQTT are still capturing the events from the physical device and publishing the MQTT to the broker but HA is missing that message while it is offline. Iā€™m not sure how to address that. Does espRFLinkMQTT need to publish those with retain as well or would that mess something else up?

Thank you all for your help. This works now. This last issue isnā€™t the end of the world but if there is a way to address it, it would be nice.

-TimG

1 Like

mate, thatā€™s what I already warned you about above

you need a message with retain: true for the state to be preserved. and a running broker.

Thatā€™s what Iā€™m looking into now. Not sure if I can do that with espRFLinkMQTT.

I guess the other way would be to actually do the automation using a Python or some other script on the MQTT broker machine to publish the retain messages if HA is down.

If HA -and- the MQTT broker are both down at the same time then I guess the zombies are going to walk freely in my houseā€¦ :wink:

yeah, they normally just send a message with retain: false. havenā€™t looked into it.

I donā€™t know if itā€™s possible. Maybe individually by parsing the brokerā€™s logs?

I was thinking more of a Python script to scribe to that/those topics and do the HA automation piece.

yes, why not?

Glad to hear it works.

Please mark my post with the Solution tag. This will help others find it quickly in the event they have a similar requirement and are seeking solutions. By tagging my post, it will appear as a link beneath your first post. It will also place a check-mark next to the topicā€™s title thereby signalling to others that it has an accepted solution.

As for your question about what happens when Home Assistant is offline, I suggest you submit a request to the author of espRFLinkMQTT to add the option of publishing retained messages.

The workaround is to create a ā€œmiddlemanā€ (running on whatever is hosting your MQTT Broker) that subscribes to rflink/# and re-publishes everything, with retained messages, to new topics (your automation will subscribe to these new topics). You can create this ā€œmiddlemanā€ using whatever technology you have available on the host (python, Node-Red, etc). FWIW, this would be a trivial Node-Red flow.