ESP home and Sonoff Bridge setup RF devices

I have flashed ESPhome and the Portisch firmware to my Sonoff Bridge
My esphome config is mostly based on the instructions here:

RF Bridge Component — ESPHome?

Everything is working as it should.

Now I want to set up my RF devices. I have a fob and some motion sensors. All of the guides that i can find on line use the remote_receiver: and remote_transmitter: components. and they return a binary code and protocol.
the rf_bridge component returns something like this:

Received RFBridge Code: sync=0x217A low=0x00FA high=0x0334 code=0x870E02

the documentation says
on_code_received Trigger

With this configuration option you can write complex automations whenever a code is received. To use the code, use a lambda template, the code and the corresponding protocol timings are available inside that lambda under the variables named code, sync, high and low.

Which i do not understand.I don’t know how to set up my RF sensors. I would really appreciate some help or a link to a tutorial.

my config is here

esphome:
  name: rf-bridge
  platform: ESP8266
  board: esp01_1m

ota:
  password: ###

wifi:
  ssid: ###
  password: ###

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Rf-Bridge Fallback Hotspot"
    password: ###

captive_portal:


api:
  services:
    - service: send_rf_code
      variables:
        sync: int
        low: int
        high: int
        code: int
      then:
        - rf_bridge.send_code:
            sync: !lambda 'return sync;'
            low: !lambda 'return low;'
            high: !lambda 'return high;'
            code: !lambda 'return code;'
    - service: learn
      then:
        - rf_bridge.learn

uart:
  tx_pin: 1
  rx_pin: 3
  baud_rate: 19200

logger:
  baud_rate: 0

sensor:
  - platform: wifi_signal
    name: Sonoff RF Bridge Wifi Signal
    update_interval: 10s

  - platform: uptime
    name: Sonoff RF Bridge Uptime

binary_sensor:
  - platform: status
    name: Sonoff RF Bridge Status


rf_bridge:
   on_code_received:
      then:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            sync: !lambda 'char buffer [10];return itoa(data.sync,buffer,16);'
            low: !lambda 'char buffer [10];return itoa(data.low,buffer,16);'
            high: !lambda 'char buffer [10];return itoa(data.high,buffer,16);'
            code: !lambda 'char buffer [10];return itoa(data.code,buffer,16);'

and a section from the log is here

[11:57:06][D][sensor:131]: 'Sonoff RF Bridge Wifi Signal': Sending state -31.00000 dBm with 0 decimals of accuracy
[11:57:10][D][rf_bridge:056]: Received RFBridge Code: sync=0x21C0 low=0x0104 high=0x0334 code=0x870E02
[11:57:11][D][rf_bridge:056]: Received RFBridge Code: sync=0x217A low=0x00DC high=0x0316 code=0x870E08
[11:57:12][D][rf_bridge:056]: Received RFBridge Code: sync=0x21A2 low=0x00F0 high=0x030C code=0x870E01
1 Like

I have worked out how to get the key fob working.
My automation is below for anyone that is interested
I would still like some help in setting up motion sensors / door sensors, etc. are these also set up as automatons or is there a way to configure them in the esphome yaml so that they are recognized as devices without having to create an automation?

id: '1631333283475'
alias: receive A button
description: ''
trigger:
  - platform: event
    event_type: esphome.rf_code_received
    id: rf code received
    event_data:
      code: '870e02'
condition: []
action:
  - service: input_boolean.toggle
    target:
      entity_id: input_boolean.a_toggle
mode: single
1 Like

did you ever get this worked out? i am at the same spot as you, but don’t think creating automations in HA is the way to go. in looking at some of the docs, i saw this page (https://esphome.io/guides/automations.html#config-lambda) that looks like you should be able to create a sensor inside ESPhome. I’ve been trying to get it to work, but have not figured it out just yet.

EDIT - scratch this, i figured it out. In case anyone else is looking, need to define a sensor in the yaml file like this:

# define the sensors
  - platform: template
    # ON -  Received RFBridge Code: sync=0x0000 low=0x0001 high=0x0010 code=0x000011
    # OFF - Received RFBridge Code:sync=0x0010 low=0x0011 high=0x0110 code=0x000111
    name: "Kitchen Window Sensor N"
    id: window_kitchen_n
    device_class: window

then add an if statement for whenever those codes are received, this needs to be inside the on_code_received block.

rf_bridge:
  on_code_received:
    then:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            sync: !lambda 'char buffer [10];return itoa(data.sync,buffer,16);'
            low: !lambda 'char buffer [10];return itoa(data.low,buffer,16);'
            high: !lambda 'char buffer [10];return itoa(data.high,buffer,16);'
            code: !lambda 'char buffer [10];return itoa(data.code,buffer,16);'

      - if:
          condition:
            lambda: |-
              return data.code == 0x000011;
          then:
            - binary_sensor.template.publish:
                id: window_kitchen_n
                state: ON

      - if:
          condition:
            lambda: |-
              return data.code == 0x000111;
          then:
            - binary_sensor.template.publish:
                id: window_kitchen_n
                state: OFF
2 Likes

@sebby @kjjordans Is there a way to see the codes being sent by the 433Mhz sensors, or could i use the codes i’m currently using in tasmota (also hex codes). I tried listening to the esphome event but i see nothing appearing.

And why does below code us : ‘return format_hex(data.sync);’ rather then ‘char buffer [10];return itoa(data.sync,buffer,16);’

rf_bridge:
  on_code_received:
    - homeassistant.event:
        event: esphome.rf_code_received
        data:
          sync: !lambda 'return format_hex(data.sync);'

Finally… how would one deal with 433Motion sensors that don’t send on or off ?

to make it easier to find the 433MHz code, do this:

text_sensor:


  - platform: template
    id: sync_datastatebridgehome
    icon: "mdi:format-vertical-align-bottom"
    name: "sync 0x0000"

  
  - platform: template
    id: low_datastatebridgehome
    icon: "mdi:format-vertical-align-bottom"
    name: "low 0x0000"
  
    
  - platform: template
    id: high_datastatebridgehome
    icon: "mdi:format-vertical-align-bottom"
    name: "high 0x0000"

      
  - platform: template
    id: code_datastatebridgehome
    icon: "mdi:format-vertical-align-bottom"
    name: "code 0x000000"

      
  - platform: template
    id: codedata_automationbridgehome
    icon: "mdi:lightbulb"
    name: "code automation bridgehome"

And add this code as well:

rf_bridge:
  uart_id: txrx
  on_code_received:
    then:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            sync: !lambda 'char buffer [10];return itoa(data.sync,buffer,16);'
            low: !lambda 'char buffer [10];return itoa(data.low,buffer,16);'
            high: !lambda 'char buffer [10];return itoa(data.high,buffer,16);'
            code: !lambda 'char buffer [10];return itoa(data.code,buffer,16);'
            
      - text_sensor.template.publish:
          id: sync_datastatebridgehome
          state: !lambda 'char buffer [10];return itoa(data.sync,buffer,16);'
      - text_sensor.template.publish:
          id: low_datastatebridgehome
          state: !lambda 'char buffer [10];return itoa(data.low,buffer,16);'
      - text_sensor.template.publish:
          id: high_datastatebridgehome
          state: !lambda 'char buffer [10];return itoa(data.high,buffer,16);'
      - text_sensor.template.publish:
          id: code_datastatebridgehome
          state: !lambda 'char buffer [10];return itoa(data.code,buffer,16);'      

      - text_sensor.template.publish:
          id: codedata_automationbridgehome
          state: !lambda 'char buffer [10];return itoa(data.code,buffer,16);'      
      - delay: 1s
      - text_sensor.template.publish:
          id: codedata_automationbridgehome
          state: "0"            

codedata_automationbridgehome will allow you to create automations based on a text sensor, making it easier to write cumbersome code

2 Likes

@lordzid @sebby @kjjordans Thanks for that.

It does work now, and if a sensor just sends one code i mitigated by adding a ‘else’ statement as well, although the states seem to alternate very quickly between closed/open/unavailable. I’m guessing i need a off_delay of some sort.

Unfortunately adding a off_delay or delay_off here seems not to work:

# define the sensors
  - platform: template
    # ON -  Received RFBridge Code: sync=0x0000 low=0x0001 high=0x0010 code=0x000011
    # OFF - Received RFBridge Code:sync=0x0010 low=0x0011 high=0x0110 code=0x000111
    name: "Kitchen Window Sensor N"
    id: window_kitchen_n
    device_class: window

Does anyone have any thoughts on that ?

you need sensor.template.publish Action And conditions “if”

So @sebby previous example used a binary_sensor and the binary_sensor.template.publish action.
Why would this not work ? Which one is preferred/works ?

I am by no means an expert (actually not even close to knowledgeable), but if it helps, here is what my config looks like at the moment. This creates 5 sensors in HA from the ESPHome Integration for a few 433mhz sensors i am using. i need to create about 25 more, hence i may rehash all this into something simpler. I am actually taking a look at @lordzid posted above as it may be a much simpler way of doing this. Anyhow, here is the config I am using currently (this is a compilation of code pulled from several places).

# Basic Config
esphome:
  name: sonoff-bridge
  #to helo with disconnects
  on_loop:
    then:
    - lambda: delay(5);

#load values on boot
  on_boot:
    then:
      - lambda: !lambda |-
          if (id(window_dining_room_e_l).state) {
            id(window_dining_room_e_l).publish_state(true);
          } else {
            id(window_dining_room_e_l).publish_state(false);
          }
          if (id(window_dining_room_e_r).state) {
            id(window_dining_room_e_r).publish_state(true);
          } else {
            id(window_dining_room_e_r).publish_state(false);
          }
          if (id(window_living_room_e_l).state) {
            id(window_living_room_e_l).publish_state(true);
          } else {
            id(window_living_room_e_l).publish_state(false);
          }
          if (id(window_living_room_e_r).state) {
            id(window_living_room_e_r).publish_state(true);
          } else {
            id(window_living_room_e_r).publish_state(false);
          }
          if (id(door_living_room).state) {
            id(door_living_room).publish_state(true);
          } else {
            id(door_living_room).publish_state(false);
          }
  

esp8266:
  board: esp01_1m

uart:
  tx_pin: 1
  rx_pin: 3
  baud_rate: 19200

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: 192.168.1.100
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    dns1: 192.168.1.1
  power_save_mode: none
  fast_connect: true

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Sonoff-Bridge Fallback Hotspot"
    password: "failback_password"

ota:
  password: "ota_password"

captive_portal:
    
web_server:
  port: 80

# Enable logging
logger:
  level: DEBUG
  baud_rate: 0

# Enable Home Assistant API
api:
  services:
    - service: send_rf_code
      variables:
        sync: int
        low: int
        high: int
        code: int
      then:
        - rf_bridge.send_code:
            sync: !lambda 'return sync;'
            low: !lambda 'return low;'
            high: !lambda 'return high;'
            code: !lambda 'return code;'
    - service: learn
      then:
        - rf_bridge.learn
    - service: advanced_learn_on
      then:
        - rf_bridge.start_advanced_sniffing
    - service: advanced_learn_off
      then:
        - rf_bridge.stop_advanced_sniffing
    - service: bucket_learn_on
      then:
        - rf_bridge.start_bucket_sniffing


rf_bridge:
  on_code_received:
    then:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            sync: !lambda 'char buffer [10];return itoa(data.sync,buffer,16);'
            low: !lambda 'char buffer [10];return itoa(data.low,buffer,16);'
            high: !lambda 'char buffer [10];return itoa(data.high,buffer,16);'
            code: !lambda 'char buffer [10];return itoa(data.code,buffer,16);'

#################################################
## Dining room windows EAST - Left and Right   ##
## Left and Right as you face them from inside ##
#################################################
    ## Living Room Window EAST
      - if:
          condition:
            lambda: |-
              return data.code == 0x17110A;
          then:
            - binary_sensor.template.publish:
                id: window_dining_room_e_l
                state: ON
      - if:
          condition:
            lambda: |-
              return data.code == 0x17110E;
          then:
            - binary_sensor.template.publish:
                id: window_dining_room_e_l
                state: OFF
      - if:
          condition:
            lambda: |-
              return data.code == 0x14000A;
          then:
            - binary_sensor.template.publish:
                id: window_dining_room_e_r
                state: ON
      - if:
          condition:
            lambda: |-
              return data.code == 0x14000E;
          then:
            - binary_sensor.template.publish:
                id: window_dining_room_e_r
                state: OFF
#################################################
## Living room windows EAST - Left and Right   ##
## Left and Right as you face them from inside ##
#################################################
      - if:
          condition:
            lambda: |-
              return data.code == 0x10000A;
          then:
            - binary_sensor.template.publish:
                id: window_living_room_e_l
                state: ON
      - if:
          condition:
            lambda: |-
              return data.code == 0x10000E;
          then:
            - binary_sensor.template.publish:
                id: window_living_room_e_l
                state: OFF
      - if:
          condition:
            lambda: |-
              return data.code == 0xAB000A;
          then:
            - binary_sensor.template.publish:
                id: window_living_room_e_r
                state: ON
      - if:
          condition:
            lambda: |-
              return data.code == 0xAB000E;
          then:
            - binary_sensor.template.publish:
                id: window_living_room_e_r
                state: OFF

########################################
## Living room door - Top Deck Door   ##
########################################
      - if:
          condition:
            lambda: |-
              return data.code == 0xA0000A;
          then:
            - binary_sensor.template.publish:
                id: door_living_room
                state: ON
      - if:
          condition:
            lambda: |-
              return data.code == 0xA0000E;
          then:
            - binary_sensor.template.publish:
                id: door_living_room
                state: OFF

  on_advanced_code_received:
    then:
      - homeassistant.event:
          event: esphome.rf_advanced_code_received
          data:
            length: !lambda 'char buffer [10];return itoa(data.length,buffer,16);'
            protocol: !lambda 'char buffer [10];return itoa(data.protocol,buffer,16);'
            code: !lambda 'return data.code;'


# Device Specific Config
# Define the sensors
binary_sensor:
  - platform: status
    name: "RF Bridge Status"

# Dining room windows facing east
  - platform: template
    name: "ESP Dining Room Window - East Left"
    id: window_dining_room_e_l
    device_class: window
  - platform: template
    name: "ESP Dining Room Window - East Right"
    id: window_dining_room_e_r
    device_class: window

# Living Room Windows facing east
  - platform: template
    name: "ESP Living Room Window - East Left"
    id: window_living_room_e_l
    device_class: window
  - platform: template
    name: "ESP Living Room Window - East Right"
    id: window_living_room_e_r
    device_class: window

# Living Room Door
  - platform: template
    name: "ESP Living Room Door"
    id: door_living_room
    device_class: door
2 Likes

So i went through this, and i think i understand it.

So you have automations based on the hex code in code sensor ? e.g. if code_datastatebridge == 0x00 then switch lightbulb on ?

What is the last sensor used for, the one that displays 0 in your lovelace screenshot? Why do i need that if i already have the code sensor ?

And finally do we agree this is the best way to do it? I guess i’m questioning because now i would need to put the exact RF code in each automation (whereas before i would just check if my binary sensor was set to ON), and also not sure how to display if a motion detector would fire for example. Or do you still use additional sensors and just use this for quickly finding the RF code ?

That’s kind’a what I said. I would add a pair of 8-Ω resistors to keep a load on the output amplifier chip to keep from overheating it.

Hey Sebby,
What is that part for ? Did you put this in to combat disconnects from the device? Getting a few of these myself lately.

yep. i can’t find the link to the forum where i read that, but that was the purpose of that delay.

Great thanks, i’ll try that myself as well. Do you still notice disconnects nowadays?

I have not noticed any disconnects.

Great topic… but I got a noob question. Can I use the buitin ESP to install the ESPhome code or I need absoluptly an external ESP connected via the UART ?