Github Devices Community Docs Blog

Help with data read out from SonOff RF Bridge to MQTT broker

I had the V0.7 openmqttgateway.bin file flashed to the sonoff RF Bridge for a while now and every time i press a 433mhz keyfob the mqtt broker would give the read out:

home/OpenMQTTGateway/version
0.7
home/OpenMQTTGateway/SRFBtoMQTT/Raw
2C06014A042E4BDAB2
home/OpenMQTTGateway/SRFBtoMQTT
4971186
home/OpenMQTTGateway/SRFBtoMQTT/Tsyn
1127
home/OpenMQTTGateway/SRFBtoMQTT/Thigh
330
home/OpenMQTTGateway/SRFBtoMQTT/Tlow
1070

So it was very simple for Home Assistant to read the data needed (4971186) and perform the requested action in my case send a test message to my phone using simplepush app.

the home assistant code in automation.yaml looks like this:

- id: '1567803474042'
  alias: System TEST
  trigger:
  - payload: '4971186'
    platform: mqtt
    topic: home/OpenMQTTGateway/SRFBtoMQTT
  condition: []
  action:
  - data:
      message: System TEST
    service: notify.nosandroid

This works…
it also works for my shed door being openned, the front door bell being pressed etc. Which then in turn i have different actions like google say commands for a caller at front door.

Anyway, i digress.

I then upgraded the firmware of the RF Bridge to 0.9.3 because if the power is disconnected from the v0.7 it might reconnect to the network and might allow recieved data to be read… so it is a bit twitchy.

I was really hoping to upgrade the firmware but now since i have done it the broker now recieves the data as:

home/OpenMQTTGateway/version
v0.9.3
home/OpenMQTTGateway/SRFBtoMQTT
{"raw":"2BE8014A042E4BDAB2","delay":1124,"val_Tlow":330,"val_Thigh":1070,"value":4971186}

now due to this all being written to one line, the H.A will no longer recognise the payload command to activate the actions.

How do i get the single line readouts that were happening on 0.7 version? (without going back to 0.7 :crazy_face: lol)

the only bits i have installed are mqtt.fx and then the web UI for the rfbridge OMG and H.A lovelace UI.

please help.

No problem, it is interesting to have some context :slight_smile:

Well, I would prefer help you to decode json instead, if this makes sense to you.
As indeed the json is the preferred way of communication.
If you are interested here is some docs:
https://docs.openmqttgateway.com/integrate/home_assistant.html#auto-discovery

Or here:

If you are ok I will take your feedback to write some docs about migration from simplePublishing to jsonPublishing.

OK, i’m willing to learn. But none of this makes sense.

I upgraded the RFbridge to 0.9.3

I logged into it’s UI and updated the wifi and mqtt settings, it rebooted and connected perfectly.

I press the keyfob and i get the one line raw data.

I have looked at the two examples that you gave, but it’s not working for me. I don’t use a binary sensor file. everything was done via automation.yaml. The mqtt and rf bridge literally worked from that one entry of text.

- id: '1567803474042'
  alias: System TEST
  trigger:
  - payload: '4971186'
    platform: mqtt
    topic: home/OpenMQTTGateway/SRFBtoMQTT
  condition: []
  action:
  - data:
      message: System TEST
    service: notify.nosandroid

So now that you have introduced this json method. what will the example need to be to use the above command in json format? and do i need to now start adding entries to other .yaml files?
This has been made a whole lot more complicated than it needed to be. as the original method was flawlessly simple.

Yes thats fine :slight_smile:

Maybe @francisp can help you on this ?

It’s ok if its full raw data one lines, i might aswell flasm tasmota as it has a better UI, a working console and stable. I came to OMG because it was a simpler and straight forward, but now its changed. So it makes sense to go with the bigger community of tasmota.

There is no need to abandon OMG for that :wink:
If you have only few automations, I think it’s easy to modify them (trigger part) to work with new messages format.
Otherwise it’s better to employ a demuxer, people discussed it a lot on HA community website (here is an example).
Let me know if you need further help.

This is how I setup my 2 rfbridges.

In automations.yaml :

- id: '1565365902541'
 alias: rfbridge_demultiplexer
 trigger:
 - platform: mqtt
   topic: home/OpenMQTTGateway/SRFBtoMQTT
 action:
 - data_template:
     payload: '{{trigger.payload_json.value}}'
   service: python_script.rfbridge_demux
- id: '1565365902888'
 alias: rfbridge_demultiplexer2
 trigger:
 - platform: mqtt
   topic: home/OpenMQTTGateway1/SRFBtoMQTT
 action:
 - data_template:
     payload: '{{trigger.payload_json.value}}'
   service: python_script.rfbridge_demux

Then the following python script :

d = { '9823336':['ikea_switch1_sensor','ON','false'],
      '9823332':['ikea_switch2_sensor','ON','false'],
      '9823330':['ikea_switch3_sensor','ON','false'],
      '9823329':['ikea_switch4_sensor','ON','false'],
      '14163857':['doorbell_sensor','ON','false'],
      '13666408':['firealarm','ON','false'],
      '564886':['garagedoor','ON','false'],
      '16726408':['lichteetkamerboven','ON','false'],
      '16765268':['impuls','ON','false'],
      '16765265':['flamingo1','ON','false'],
      '352003':['buurvrouw','ON','false'],
      '1213858':['lichtwaskot','ON','false'],
      '1224498':['buurvrouw1','ON','false'],
      '16762193':['impulsa','OFF','false'],
      '16762196':['impulsa','ON','false'],
      '2291358':['voordeur','ON','true'],
      '2291351':['voordeur','OFF','true'],
      '152232' :['schuindak','ON','false']
    }


p = data.get('payload')

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

Then I check topic on mqtt sensor/…

- id: '1565365902539'
  alias: Ikea switch2
  trigger:
  - payload: 'ON'
    platform: mqtt
    topic: sensor/ikea_switch2_sensor
  condition: []
  action:
  - data:
      entity_id: switch.ikeaoutlet2_switch
    service: switch.toggle

Could be

- alias: rfbridge_demultiplexer
  trigger:
    - platform: mqtt
      topic: home/OpenMQTTGateway/SRFBtoMQTT
    - platform: mqtt
      topic: home/OpenMQTTGateway1/SRFBtoMQTT
  action:
    service: python_script.rfbridge_demux
    data_template:
      payload: '{{ trigger.payload_json.value }}'

@AhmadK @francisp thanks for the help !

Where does the Python Script stuff go?
I tried putting it in the configuration.yaml and HA didn’t reboot. I had to manually remove it before trying to boot HA back up again. <–edit: I then took this out of the .yaml and done the below instead.

So far I have followed first setup of python: https://www.home-assistant.io/integrations/python_script/

I have put python_script: into it’s own line in the configuration.yaml

In the config area i have created a new folder called: python_scripts

I have then created hello_world.py file in the said new python folder.

Then I have placed this into the .py file and saved:

name = data.get("name", "world")
logger.info("Hello %s", name)
hass.bus.fire(name, {"wow": "from a Python script!"})

After i rebooted and then went to Developer tools and services tab, in the services drop down list i selected: python_script.hello_world and in service data entered: name: you

then i click call service…
What am i waiting for to happen? nothing gets explained after that? Do i need to keep doing this again and again? after each reboot? These HA examples are crap with information and don’t explain why each section is placed in a folder,file,service etc.

Do i even need a hello_world.py? is it relevant to what im trying to do?

Anyway after this i then place fracissp example above and done this:

automations.yaml

- id: '1565365902541'
  alias: rfbridge_demultiplexer
  trigger:
  - platform: mqtt
    topic: home/OpenMQTTGateway/SRFBtoMQTT
  action:
  - data_template:
      payload: '{{trigger.payload_json.value}}'
    service: python_script.rfbridge_demux

hello_world.py <---- Is this even the correct place to put your example code?

name = data.get("name", "world")
logger.info("Hello %s", name)
hass.bus.fire(name, {"wow": "from a Python script!"})


d = { '4971186':['System_TEST','ON','false'],
      '2291358':['voordeur','ON','true'],
      '2291351':['voordeur','OFF','true'],
    }


p = data.get('payload')

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

automations.yaml <-----Is this where i place the “check topic on mqtt sensor/” information?

- id: '1565365902539'
  alias: System TEST
  trigger:
  - payload: 'ON'
    platform: mqtt
    topic: sensor/system_TEST
  condition: []
  action:
  - data:
      message: System TEST
    service: notify.nosandroid

My RF bridge mqtt is connecting fine with latest firmware, It’s named to output as: OpenMQTTGateway not OpenMQTTGateway_SRFB in the UI fn the rf bridge AP setup page.

Im getting the mqtt broker readout once i press the my tester RF button:

home/OpenMQTTGateway/SRFBtoMQTT
{"raw":"2BFC013604104BDAB2","delay":1126,"val_Tlow":310,"val_Thigh":1040,"value":4971186}

Above is so you have some context to go on and I have the below code in my configuration.yaml:

notify:
  - name: nosandroid
    platform: simplepush
    device_key: <my unique simplepush  ID>

so now you have all the information to see exactly what i am trying to do. once i know this works i can start adding all my automations that USED to work fine on the old RF firmware, into the format of this new firmware.

Also as a last question, do i need any certain addon installed for python? or is it all part and parcel of HA being installed and running? is there a way to check?

thank you.

You put python_script: in configuration.yaml

Then in /config/python_scripts you create a file rfbridge_demux.py with the content from my post

Then you restart HA (otherwise HA does not see the script)

Then you can add the automations.

That is standard HA. No add-on to install

Thank you so much francisp this really is helping :slight_smile: and i appreciate you dumbing down your explanations for me to understand :slight_smile:

OK I am now getting somewhere, in the overview I have the automations appear. I can manual execute them and the system test does indeed notify my phone.

When I press the keyfob button the mqtt reports the payload AND indeed the sensor/system_test payload “ON” :smiley: :smiley:

BUT, somewhere between the two, the automation is NOT automatically executing. :frowning: i think im missing something.

mqtt.fx reports:

home/OpenMQTTGateway/SRFBtoMQTT
{"raw":"2BDE0154042E4BDAB2","delay":1123,"val_Tlow":340,"val_Thigh":1070,"value":4971186}

sensor/System_TEST
ON

Scratch that, was an incorrectly type line (sensor/system_TEST has a capital “S” for System… whooops!) :smiley:

IT WORKS!!!
@AhmadK @1technophile huge thanks for keeping the information coming,

HUGE thanks and eternally indebted to @francisp for the examples and keeping with me on this :slight_smile:

Superb guys, thank you.

1 Like