Home Assistant Community

Toggle state of a binary switch on RF Bridge via MQTT

templates
mqtt
Tags: #<Tag:0x00007fcf504ab290> #<Tag:0x00007fcf504ab0b0>
#1

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?

#2

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%}"
#3

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.

#4

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
#5

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?

#6

@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
#7

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

#8
  • 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.