Toggle state of a binary switch on RF Bridge via MQTT

Hello friends,

I’m new to Home Assistant and using a Hassio installation on rPi3+ with Sonoff RF bridge. I’m using a binary sensor that sends a single code via MQTT. What I want is to change the state of this sensor from ‘on’ to ‘off’ each time Home Assistant receives this code. So far, I can see the sensor state change from ‘off’ to ‘on’ once only before having to reboot the system. I came across this article which seems to create a dummy variable to store the state in and I don’t think this is what I’m after exactly. Then came across this in the MQTT docs from home assistant and have placed this code in my configuration.yaml file. Here is the code I’m using:

binary_sensor:    
- platform: mqtt
      state_topic: "tele/RF_Bridge/RESULT"
      name: 'Cottage Bedroom Window #2'
      value_template: '{value_json.RfReceived.Data}'
      payload_on: 'E1AF46'
      device_class: door
      optimistic: true
      qos: 1
      force_update: true
      retain: false
      
    - platform: mqtt
      state_topic: "tele/RF_Bridge/RESULT"
      value_template: "{%if is_state(binary_sensor.cottage_bedroom_window_2,\"on\")-%}OFF{%-else-%}ON{%-endif%}"

Where am I going wrong here?

To clarify, I’m trying to understand why this statement is not working:

value_template: "{%if is_state(binary_sensor.cottage_bedroom_window_2,\"on\")-%}OFF{%-else-%}ON{%-endif%}"

I had a similar issue a few days ago and couldn’t get the value template to work either - that’s how I ended up with this syntax:

    - service: mqtt.publish
      data_template:
        topic: 'zigbee2mqtt/XA_Btn_01'
        payload: >-
          {
           "click": "" 
          }

You might also want to take a look at the post that that helped me figure out my issue here:

I know that these are both about mqtt.publish - maybe it’s of help anyway.

The following solution has been tested and proven to work.

What I suggest is using an automation.

  • It subscribes to the topic ‘tele/RF_Bridge/RESULT’.
  • When it detects ‘E1AF46’ it publishes ‘ON’ or ‘OFF’ to another topic called ‘sensor/window2’.
  • The choice of ‘ON’ or ‘OFF’ will be the opposite state of your binary_sensor’s current state.

Automation:

- alias: 'Bedroom Window 2 Toggle'
  trigger:
    platform: mqtt
    topic: 'tele/RF_Bridge/RESULT'
  condition:
    condition: template
    value_template: "{{ trigger.payload_json.RfReceived.Data == 'E1AF46' }}"
  action:
    service: mqtt.publish
    data_template:
      topic: 'sensor/window2'
      payload: "{{ 'OFF' if states('binary_sensor.cottage_bedroom_window_2') == 'on' else 'ON' }}"
      retain: true
      qos: 1

Your binary_sensor is simply subscribed to ‘sensor/window2’. No other options are required.

Binary_sensor:

- platform: mqtt
  state_topic: 'sensor/window2'
  name: 'Cottage Bedroom Window 2'
  device_class: door
1 Like

This worked exactly as I was hoping. I have 13 sensors, so I’m assuming I’ll need to do the same for each one. Correct?

@123 It looks like I spoke too soon, It worked for a while but after I rebooted and upgraded to Mosquitto 4.2, it broke. It looks like the broker is still receiving messages because this is in the log:

19-05-18 17:43:12 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on tele/RF_Bridge/RESULT: b'{"RfReceived":{"Sync":11470,"Low":370,"High":1110,"Data":"E1AF46","RfKey":"None"}}

Here is what is in my automations.yaml file:

   - alias: 'Bedroom Window 2 Toggle'
      trigger:
        platform: mqtt
        topic: 'tele/RF_Bridge/RESULT'
      condition:
        condition: template
        value_template: "{{ trigger.payload_json.RfReceived.Data == 'E1AF46' }}"
      action:
        service: mqtt.publish
        data_template:
          topic: 'sensor/window2'
          payload: "{{ 'OFF' if states('binary_sensor.cottage_bedroom_window_2') == 'on' else 'ON' }}"
          retain: true
          qos: 1

And this is my code for the binary sensors in the configuration.yaml file:

binary_sensor:
  -  platform: mqtt
 state_topic: 'sensor/window2'
 name: 'Cottage Bedroom Window 2'
 device_class: door
 qos: 1
 
  -  platform: mqtt
 state_topic: "tele/RF_Bridge/RESULT"
 name: 'Garage Door'
 value_template: '{{value_json.RfReceived.Data}}'
 payload_on: 'E1AF46'
 payload_off: 'D54C1Eoff'
 device_class: door
 qos: 1

I think I’m having spacing issues. Are the ‘-alias’ and ‘action’ lines supposed to be indented in the same way as ‘-platform’ even though they are in separate files?

2019-05-18_1051

  • The code works.
  • You upgraded the Mosquitto broker.
  • The code no longer works.

Why are you concluding the problem is with the code? Unless you also made changes to the code …

None of the sensor configuration examples you’ve provided is indented properly.

This should work:

binary_sensor:
  - platform: mqtt
    state_topic: 'sensor/window2'
    name: 'Cottage Bedroom Window 2'
    device_class: door
    qos: 1
 
  - platform: mqtt
    state_topic: "tele/RF_Bridge/RESULT"
    name: 'Garage Door'
    value_template: '{{value_json.RfReceived.Data}}'
    payload_on: 'E1AF46'
    payload_off: 'D54C1Eoff'
    device_class: door
    qos: 1

It’s one space between - and platform, not two spaces.

I have been trying to get one of these single code banggood rf switches to work using the example from Taras.

I can press the switch and the bridge receives the packet and publishes the word ON to the binary sensor but how do I get it to do the opposite when the switch is pressed again it only seems to publish ON if its state is OFF

here is my code

automation.yaml

- id: '1576889107598'
  alias: 'Single Toggle'
  description: single switch toggles
  trigger:
    platform: mqtt
    topic: 'tele/rfbridge/RESULT'
  condition:
    condition: template
    value_template: "{{ trigger.payload_json.RfReceived.Data == 'FC71D8' }}"
  action:
    service: mqtt.publish
    data_template:
      topic: 'switch/single1'
      payload: "{{ 'OFF' if states('binary_sensor.single') == 'on' else 'ON' }}"
      retain: true
      qos: 1

configuration.yaml

          state_topic: 'switch/single1'
          name: 'Switch1'

How do I make it toggle state

These single code switches only have one code, so they will always send ‘ON’. What do you want to do with them ? I have a bunch of them, but I never tried to make them ‘OFF’, just use them in automations.

Instead of a switch, use an input_boolean to switch, and add an off_delay to your binary sensor.

I wanted to replace my kitchen light switch.
I figured it out literally just now in the end doing it in automation.yaml using toggle

- id: '88'
  alias: AllKitchenlights
  hide_entity: true
  trigger:
    platform: mqtt
    topic: tele/rfbridge/RESULT
  condition:
    condition: template
    value_template: '{{ trigger.payload_json.RfReceived.Data == "FF452E" }}'
  action:
  - data: {}
    entity_id: group.kitchen_lights
    service: light.toggle