Turn Sonoff Switch on from RF button

Hi,

Very basic question I’m sure, but I’m struggling to get my head around this. I have a Sonoff Basic switch as well as a Sonoff RF bridge with a four button remote.

I have both flashed with Tasmota and talking to Home Assistant - I can control the switch from the GUI turning on and off, and I can see the button state change - I’m using the off_delay parameter to reset the state to off after one second. I’m trying to have home assistant change the state of the switch when the button is pressed (eg if it’s on turn it off, if it’s off turn it on), I have this in configuration.yaml at the moment:

switch:
platform: mqtt
name: “Sonoff_Switch_01”
command_topic: “cmnd/sonoff_basic_1/power”
state_topic: “stat/sonoff_basic_1/POWER”
qos: 1
payload_on: “323234”
payload_off: “323234off”
retain: true

binary_sensor:

  • platform: mqtt
    state_topic: “tele/RF_Bridge/RESULT”
    value_template: ‘{{value_json.RfReceived.Data}}’
    payload_on: ‘323234’
    payload_off: ‘323234off’
    name: “Button_A”
    alias: “Button_A”
    off_delay: 1

I’m struggling to see how to bring the two together - could use some guidance!

Thanks,

Rob

why is the payload for sonoff basic same as payload for the rf button?

switch should be defined as in tasmota wiki:

switch:
  - platform: mqtt
    name: "Sonoff power"
    state_topic: "stat/sonoff/RESULT"
    value_template: "{{ value_json.POWER }}"
    command_topic: "cmnd/sonoff/POWER"
    availability_topic: "tele/sonoff/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

and you need an automation that turns or toggles that switch when you receive an mqtt message from the bridge after pressing the button

Thanks for that - I’ve corrected that now, got a bit further, now have an automation which turns the sonoff switch on:

  - alias: "Turn switch on"
    trigger:
      platform: state
      entity_id: binary_sensor.Button_A
      to: 'on'
    condition:
        condition: state
        entity_id: switch.Sonoff_Switch_01
        state: 'off'
    action:
       service: switch.turn_on
        data:
            entity_id: switch.Sonoff_Switch_01
            

So I think I’m nearly there, the final piece I’d like to figure out is how to tweak the condition - eg if the sonoff switch state is currently off then turn it on, however if it’s currently on then turn it off, in powershell I’d do an If statement on to check the state of switch.Sonoff_Switch_01 and fire actions based on that.

just use switch.toggle so itll figure itself out since it toggles the state

or

  condition:
    condition: state
    entity_id: switch.Sonoff_Switch_01
    state: 'on'

Thanks, I’ve got a working solution, in the spirit of sharing this is my working configuration:

configuration.yaml:

switch:
    platform: mqtt
    name: "Sonoff_Switch_01"
    command_topic: "cmnd/sonoff_basic_1/power"
    state_topic: "stat/sonoff_basic_1/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
    
    
binary_sensor:
  - platform: mqtt
    state_topic: "tele/RF_Bridge/RESULT"
    value_template: '{{value_json.RfReceived.Data}}'
    payload_on: '323234'
    payload_off: '323234off'
    name: "Button_A"
    alias: "Button_A"
    off_delay: 1

automations.yaml

  - id: change_switch_01_state
    alias: "Change switch 01 state"
    trigger:
      platform: state
      entity_id: binary_sensor.Button_A
      to: 'on'
    action:
     - service: switch.toggle
       data:
        entity_id: switch.Sonoff_Switch_01
3 Likes

thanks for the info. i have been trying for 6 hours to get a simple sonoff basic switch to turn on with an RF remote through the sonoff bridge.
if i go into dev tools and try to call service of the switch entity nothing happens also. where is this broken !!!

the Mosquitto broker addon is indicating things are connected.
1564821541: New connection from 192.168.1.53 on port 1883.
1564821541: New connection from 192.168.1.51 on port 1883.
1564821541: New client connected from 192.168.1.53 as RF_Bridge (p2, c1, k30, u’sonoff’).
1564821541: New client connected from 192.168.1.51 as sonoff_51 (p2, c1, k30, u’sonoff’).

mqtt:
  broker: core-mosquitto
  username: sonoff
  password: ***

switch:
    platform: mqtt
    name: "sonoff_51"
    command_topic: "cmnd/sonoff/power"
    state_topic: "stat/sonoff/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
    

binary_sensor:
  - platform: mqtt
    state_topic: "cmnd/RF_Bridge/RESULT"
    value_template: '{{value_json.RfReceived.Data}}'
    payload_on: 'BCACE2'
    payload_off: 'BCACE2off'
    name: "Button_A"
    alias: "Button_A"
    off_delay: 1


  - id: change_switch_01_state
    alias: "Change switch 01 state"
    trigger:
      platform: state
      entity_id: binary_sensor.Button_A
      to: 'on'
    action:
     - service: switch.toggle
       data:
        entity_id: switch.sonoff_51

After many more hours and many more videos ive managed to get switches controlled through the RF remote. . . i ended up abandoning the yaml file editing and using all node red.
during this time i worked out that in the switch config the prefix was different.

image
however when i tried to change it around to %prefix%/%topic%/ it never saved it that way.
so i just changed the topic to “sonoff_51/cmnd/power” and it worked. . .

big thanks to digiblurDIY . this video and node red MQTT : https://www.youtube.com/watch?v=0jdGs-213qI