Sonoff RF Binary Sensor

Morning All,

I have a motor gate with a Sonoff RF door sensor and a magnetic contact that I am trying to get working. I currently have MQTT messages from the RF sensor to home assistant via an RF Bridge working fine. When the gate opens the home assistant binary sensor is turned on and remains on until I restart hassio again.

What I want is for the MQTT message to toggle the state of the sensor. Currently my yaml file looks like this but it was wishful thinking to think that the same payload would toggle the switch :slight_smile:

    - platform: mqtt
      name: "Front Gate"
      state_topic: "tele/rfbridge/RESULT"
      value_template: '{{value_json.RfReceived.Data}}'
      payload_on: "DC0978"
      payload_off: "DC0978"
      device_class: garage_door
      qos: 1

I have been reading up on the MQTT sensors and I see that there is an “IF” option that could be added to the value template field. Is there a way of combining this with my current config to cause a toggle action on the sensor when an on payload is received?

  - platform: mqtt
    state_topic: "lab_button/cmnd/POWER"
    value_template: "{%if is_state(entity_id,\"on\")-%}OFF{%-else-%}ON{%-endif%}"

Thank you.

You may want to have a read up on Strategy 2

All my RF Sensors are setup this way. You can use the setup for both sensors that give On / Off and sensors that only give one signal.

1 Like

@wills106, thank you.

I followed the instructions for strategy 1 and played around with the value_template to get to the following result that is working swimmingly for me. When the gate opens, the sensor state is changed to on and when the gate closes it toggles to off. I will look at strategy 2 in the future as that seems interesting as well.

    - platform: mqtt
      name: "Front Gate"
      state_topic: "tele/rfbridge/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'DC0978' %}{% if states('binary_sensor.front_gate') == 'off' %}{{'ON'}}{% else %}{{'OFF'}}{% endif %}{% endif %}
      device_class: garage_door
      qos: 1

Glad you got it solved.

I would recommend you look at Solution 2 when you get time. Handy for when you have multiple sensors and helps to retain states during restarts etc.