Sonoff RF Bridge. Strategies for receiving data

Hello Taras,

I finally got it working. The problem was just a comma in the python scripts code which messed up everything .Thank you for all your help.

At some point you suggested the Publish off automation. When i start home assistant the sensors state is already off. Do I need to use the automation?

Thank you again

Hello Ash,

I just deleted everything and followed Taras Solution 2. It seems that the problem was a missing comma in the python script which caused a problem in the sensors .

Now it is ok

Thank you

If the state is off at startup then the suggested automation isn’t necessary.

The esphome firmware appears to be using the homeassistant events. Are those mqtt based?

Does anybody have any experience with it?

1 Like

Typically the best choice is to use the native api for direct (bi-directional) communication with home assistant when using esphome (so no mqtt broker or any configuration/“strategies” like described in this post are actually needed)

I did, you can just trigger anything interacting with ha from the esphome device directly. I use this for virtually everything (including IR and RF).

Also someone (don’t find any link right now - sorry) actually “abused” the tags in ha (with it’s nice management) :point_down:

Home Assistant has a dedicated panel that allows you to manage your tags. You can add names, automate or delete them

to handle all his IR or RF payloads/data.

In esphome the easiest way to make use of it is the tag-scanned-action :rocket:

# In some trigger
on_...:
  # Simple
  - homeassistant.tag_scanned: some-tag
1 Like

Just now saw that you were referring to the Home Assistant Events. From what I know that’s not possible to use them with mqtt but only by utilizing the native api (from esphome).

:point_down:

1 Like

First off, I’d like to say I’m very thankful for this script and the instructions on implementing it. I’ve used soon after this thread we as created and it’s integral to my setup.

I’m facing a problem with memory leak since updating to 2022.7 (2022.7.1 through to .7). I suspect it is due to the python 3.10 change because nothing else in the breaking changes affects me. I doubled all my custom integrations (HACS) for a day and the problem persisted. The last thing I can think of is this python script.

Is anyone aware whether this script would malfunction on the upgraded python library 3.10 on 2022.7. I don’t understand coding libraries so I’m appealing to the knowledge and experience here.

Thanks in advance
In case it matters: Rasp Pi 4, 4GB, Homeassistant OS.

First thing, thank you so much for the author and commenters on this topic.

I received several cheap RF door sensors, and the MQTT messages coming via rtl_433 would be joined in one topic because the devices use different identifiers, in this case ‘dipswitch’ which is part of the payload. After some messing about with strategy 1, I moved to strategy 2 which is so much more scalable and a good experience to go through to understand the idea behind demuxing.

Setup:

  • HW: HA running on an Intel NUC with RTL_433 running on a Rpi3b.
  • SW: Home Assistant 2022.7.7, Supervisor 2022.07.0, Operating System 8.4

I’ve put the two files on github: RF door sensor MQTT demultiplexing/demux using a python script · GitHub

Python script

d = { '-o-+-oo-o':['sensor01','true'],
      '--+--o+++':['sensor02','true'],
      'o++--o--o':['sensor03','true'],
      '--o--o-+-':['sensor04','true']
    }
e = { '10R':['on'],
      '00L?':['off']
}

p = data.get('payload')              # I must keep the dict and not str() convert it
p1 = p['dipswitch']  
p2 = p['rbutton']  

if p is not None:
  if p1 in d.keys():
    service_data = {'topic':'rtl_433/Cardin-S466/{}'.format(d[p1][0]), 'payload':"{}".format(e[p2][0]), 'qos':1, 'retain':"{}".format(d[p1][1])}
  elif p1 == "":
    service_data = {'topic':'rtl_433/Cardin-S466/blank', 'payload':'{}'.format(p), 'qos':1, 'retain':'false'}
  else:
    service_data = {'topic':'rtl_433/Cardin-S466/unknown', 'payload':'{}'.format(p).str() , 'qos':1, 'retain':'false'}
    logger.warning('<rfbridge_demux> Received unknown RF command: {}'.format(p))
  hass.services.call('mqtt', 'publish', service_data, False)

mqtt yaml file

# Garage Side Door
- unique_id: 'door_garagesidedoor'
  name: 'Garage Side Door'
  device_class: 'door'
  state_topic: 'rtl_433/Cardin-S466/sensor01'
  value_template: '{{ value }}'
  payload_on: 'off'
  payload_off: 'on'
# Front Door
- unique_id: 'door_frontdoor'
  name: 'Front Door'
  device_class: 'door'
  state_topic: 'rtl_433/Cardin-S466/sensor02'
  value_template: '{{ value }}'
  payload_on: 'off'
  payload_off: 'on'
etc...

Hope this helps anyone.

1 Like

Hi,

I have configured everything so far, but the status of “binary_sensor” remains unchanged at “unknown”.
The automation displays the correct status, but the sensor does not receive the information.

confuguration.yaml

#Sonoff
mqtt:
  binary_sensor:
    - name: 'Tor'
      unique_id: "Tor"
      state_topic: 'home/sensor1'
      device_class: garage_door

automation.yaml

  alias: Tor Status
  trigger:
  - platform: mqtt
    topic: tele/sonoff_bridge/RESULT
  action:
  - service: python_script.rfbridge_demux
    data:
      payload: '{{trigger.payload_json.RfReceived.Data}}'

/config/python_scripts/rfbridge_demux.py

d = { 'C1615E':['sensor1','ON','true'],
      'C16157':['sensor1','OFF','true']
    }

p = str(data.get('payload'))

if p is not None:
  if p in d.keys():
    service_data = {'topic':'home/{}'.format(d[p][0]), 'payload':'{}'.format(d[p][1]), 'qos':0, 'retain':'{}'.format(d[p][2])}
  else:
    service_data = {'topic':'home/unknown', 'payload':'{}'.format(p), 'qos':0, 'retain':'false'}
    logger.warning('<rfbridge_demux> Received unknown RF command: {}'.format(p))
  hass.services.call('mqtt', 'publish', service_data, False)

My old config - without python demux

#Sonoff
binary_sensor:
  - platform: mqtt
    name: 'Tor'
    state_topic: 'tele/sonoff_bridge/RESULT'
    value_template: '{{ value_json.RfReceived.Data }}'
    payload_on: "C1615E"
    payload_off: "C16157"
    device_class: garage_door
    qos: 1

Does anyone have an idea where my mistake is?

Use an MQTT client (like MQTT Explorer) to examine what is published to home/sensor1.

@123 thanks for your work explaining the strategies. Is there any way of having resiliency of messages by having multiple sonoff bridges ? I feel i am missing 433mhz signals at times, which would be understandable, as i only have on of them.

Ofcourse then you have to deal with possible multiple messages…

Appreciate your input.

You can have multiple bridges. I have five and it works extremely well.

See this post and the one after.

Boy am I glad I found this thread. I have been sing rf sensor for a long time and it always annoyed me that they would go back to unknown each time I restarted HA.

Now my door sensor are all good and report the correct state (unless of course their state change while HA is rebooting)

My movement detector still shows unknown after a restart but I assume this is expected because HA is not able to read the current state until it changes.

Am I correct in my assumption?

If you are using Strategy #2 and your motion detector reports both its on and off states, the strategy offers a means of publishling the motion detector’s value as a retained message (i.e. stored on the broker). Therefore the stored value is available to Home Assistant immediately upon startup.

From the first post:

Because the door sensor reports both of its states, the automation can use retain: true when publishing to the contact sensor’s topic. When Home Assistant restarts, it will re-subscribe to the contact sensor topic’s and receive its current (retained) state from the broker.

Yes I am using the second strategy.

The motion detector only reports on so I set the retain to false in the script and I guess that it why it’s state is set to unknown after a restart.

Hi,
most of my contact sensors report the on and off state. Do you mind tell me where to add the retain: true to retain the states as they all show unknown on boot. Do I add it in the action of my rfbride_demultiplexer like this

service: python_script.rfbridge_demux
data_template:
  payload: "{{trigger.payload_json.RfReceived.Data}}"
  retain: true

Thank you

In the demux python script there is a variable d where you store all the sensor you have. Like this example

d = { 'C1615E':['sensor1','ON','true'],
      'C16157':['sensor1','OFF','true']

here you create a sensor called sensor1 that has 2 code (C1615E and C16157) and the retain is set to true so that it will remember its previous after a reboot.

It’s described in the first post. There’s an example of the python_script containing a dictionary and its format explained as follows:

Given that the explanation wasn’t clear to you, can you suggest how I can improve it?

Thank you, I don’t know what I was thinking. I appreciate the help.

Thank you! it makes sense now. It’s clear as day, no need to improve it, I just missed this part.

1 Like