433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Moderator: leecollings

spacestation
Posts: 2
Joined: Tuesday 07 August 2018 19:23
Target OS: Linux
Domoticz version:
Contact:

433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by spacestation »

Hi all, I am planning to install some Sonoff Wifi switches and smart plugs in a room of my apartment. My initial idea was to control them using these 433Mhz wireless touch remotes which look like ordinary wall switches -

https://www.aliexpress.com/item/Vhome-W ... 4c4dcWQeNJ

Ideally these switches will send signals to a Sonoff RF bridge which will then communicate them to Domoticz via MQTT over wifi. In that way I can easily assign each wireless touch remote to a one or a group of devices in Domoticz.

I flashed the Sonoff RF bridge with the latest Tasmota firmware 6.1.1. The wireless remote switches were also all successfully learnt by the Sonoff RF bridge. When buttons on the remote were pressed. I could see MQTT messages in the console associated with those button. The problems I am facing now are:

1. The MQTT messages from the RF bridge were not seen in Domoticz. I gathered that this is due to the Sonoff RF bridge MQTT message format not being recognized by Domoticz? A Tasmota firmware modification is needed?

2. Assuming the previous problem was solved. How do I setup Domoticz such that a button press on the wireless remote toggles the state of a device, group, and scene?

I seem to get the feeling that the feature I mentioned is simply not possible with the current Tasmota firmware. Most questions on the forum talked about linking sensors via the RF bridge to Domotizc (kind or a similar to what I want to do here) or Domoticz controlling 433Mhz devices via the RF bridge.

Thank you.

Unfortunately I cannot provide any screenshots of the MQTT messages from the RF bridge. After a failed OTA update, my Sonoff RF bridge is completely dead. Even a direct flashing via a USB to serial connection didn't work. I have ordered a few more, should be here in a week or two...
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by MikeF »

When I saw this, I immediately thought: 'Node-RED'.

As I've got a number of Sonoff (basic switch) modules, which I've flashed with Tasmota and which I've got linked to Domoticz (controlling lights), I decided to experiment with this. I bought a Sonoff RF Bridge on eBay, flashed it and started to play around. As you will have seen in the console, the RF Bridge produces a message of this format when a key of a paired remote is pressed:

Code: Select all

tele/sonoffB/RESULT = {"RfReceived":{"Sync":10280,"Low":380,"High":940,"Data":"514115","RfKey":1}}
(sonoffB is the topic for my bridge.)

I realised that it would then be possible to build a Node-RED flow to control a number of Domoticz devices based on the value of RfKey, using the domoticz/in MQTT topic. (If you're not familiar with Node-RED - and I'm a bit of a novice - there's enough in the Domoticz wiki and Node-RED website to get you started.)

So far, I've just learned the buttons on a 2-button HomeEasy 433MHz remote, and created a Node-RED flow to control 2 light switches in Domoticz. This is what I've come up with:
Image
This is the corresponding code, which can be imported into Node-RED:
Spoiler: show

Code: Select all

[{"id":"a86651d0.e0e2c","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"50c37c48.99cb14","type":"mqtt in","z":"a86651d0.e0e2c","name":"sonoffB","topic":"tele/sonoffB/RESULT","qos":"2","broker":"51e6c686.61a968","x":110,"y":100,"wires":[["57a6cdcf.acac84"]]},{"id":"57a6cdcf.acac84","type":"function","z":"a86651d0.e0e2c","name":"SaveTopic","func":"var msg2 = {};\nmsg2.payload=msg.payload;\nmsg2.stat=msg.payload;\nmsg2.topic=msg.topic;\nreturn msg2;","outputs":1,"noerr":0,"x":270,"y":100,"wires":[["23c61085.d83f6"]]},{"id":"23c61085.d83f6","type":"function","z":"a86651d0.e0e2c","name":"GetRFKey","func":"var msg2 = {};\n//var idx = {};\n//msg2.payload=msg.payload;\n//msg2.stat=msg.stat;\ntabresult = JSON.parse(msg.payload);\nrfkey = tabresult.RfReceived.RfKey; \nmsg2.payload = rfkey;\nreturn msg2;","outputs":1,"noerr":0,"x":470,"y":100,"wires":[["f1fe824c.d13a4","ce2be0ce.bf24b8"]]},{"id":"f1fe824c.d13a4","type":"debug","z":"a86651d0.e0e2c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":40,"wires":[]},{"id":"ce2be0ce.bf24b8","type":"switch","z":"a86651d0.e0e2c","name":"ChooseRFKey","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":160,"y":220,"wires":[["147d3ed7.c4fbf1"],["82f6cb0a.852b48"]]},{"id":"147d3ed7.c4fbf1","type":"function","z":"a86651d0.e0e2c","name":"PrepDomoticzCmd1","func":"msg2 = {};\npayload = {};\nmsg2.payload=payload;\nmsg2.payload.command = \"switchlight\";\nmsg2.payload.idx = 118;\nmsg2.payload.switchcmd = \"Toggle\";\nmsg2.payload.level = 100;\nmsg2.topic=\"\";\nreturn msg2;","outputs":1,"noerr":0,"x":440,"y":200,"wires":[["64e5982b.aba568"]]},{"id":"64e5982b.aba568","type":"mqtt out","z":"a86651d0.e0e2c","name":"","topic":"domoticz/in","qos":"","retain":"","broker":"51e6c686.61a968","x":650,"y":200,"wires":[]},{"id":"a8bbaa52.46ed38","type":"mqtt out","z":"a86651d0.e0e2c","name":"","topic":"domoticz/in","qos":"","retain":"","broker":"51e6c686.61a968","x":650,"y":260,"wires":[]},{"id":"82f6cb0a.852b48","type":"function","z":"a86651d0.e0e2c","name":"PrepDomoticzCmd2","func":"msg2 = {};\npayload = {};\nmsg2.payload=payload;\nmsg2.payload.command = \"switchlight\";\nmsg2.payload.idx = 221;\nmsg2.payload.switchcmd = \"Toggle\";\nmsg2.payload.level = 100;\nmsg2.topic=\"\";\nreturn msg2;","outputs":1,"noerr":0,"x":440,"y":260,"wires":[["a8bbaa52.46ed38"]]},{"id":"51e6c686.61a968","type":"mqtt-broker","z":"","name":"Domoticz","broker":"192.168.0.63","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
This can be extended for more Domoticz devices, by adding more outputs to ChooseRFKey, and duplicating the PrepDomoticzCmd<x> functions - each one provides the command for each specific Domoticz device (again, see the Domoticz wiki).

The big advantage of this is that, using the Sonoff RF Bridge, you can control Domoticz devices* - e.g., light switches - using an RF remote.
(* these can be non-Sonoff devices as well as Sonoff).
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by MikeF »

... or you can do it in Python. I've produced an example Python script - see below.

Instead of publishing to the domoticz/in MQTT topic, the script gets the rfkey number (by subscribing to the tele/<sonoff>/RESULT topic), and uses the more 'conventional' JSON API to send commands to Domoticz.

Again, this script has only been set up for two buttons - it can be extended by adding more idx's (and different Domoticz commands - I've used switchlight and Toggle) in on_message.
Spoiler: show

Code: Select all

#!/usr/bin/env python

'''
Python script to read rfkey number from Sonoff 433MHz RF Bridge
and control corresponding Domoticz.
This example only set up for 2 devices; extend by adding idx's in on_message.
(Not very pretty code!)
'''

import paho.mqtt.client as mqtt
import time
import json
import requests

### substitute your values here ###
broker_address="<MQTT broker>" # port = 1883 by default
topic = "<sonoff topic>"
dom_cmd = "http://<domoticz url>:<domoticz port>/json.htm?type=command&param=switchlight&switchcmd=Toggle"
idx1 = <idx of Domoticz device 1>
idx2 = <idx of Domoticz device 2>
###################################

def on_message(client, userdata, message):
    print "message received " ,str(message.payload.decode("utf-8"))
    jsonData = json.loads(message.payload)
    rfkey = jsonData['RfReceived']['RfKey']
    print rfkey
    if rfkey == 1:
    	idx = idx1
    elif rfkey == 2: 
    	idx = idx2
    else: 
    	return
    
    url = dom_cmd + "&idx=" + str(idx)
    print url
    r = requests.get(url)
    
    #print "message topic=",message.topic
    #print "message qos=",message.qos
    #print "message retain flag=",message.retain

print("creating new instance")

client = mqtt.Client("P1") #create new instance
client.on_message=on_message #attach function to callback

print("connecting to broker")
client.connect(broker_address) #connect to broker

client.loop_start() #start the loop

print"Subscribing to topic " + topic
client.subscribe("tele/" + topic + "/RESULT")

time.sleep(2) # wait
client.loop_forever() #loop forever

MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by MikeF »

@spacestation: did you get anywhere with this?
spacestation
Posts: 2
Joined: Tuesday 07 August 2018 19:23
Target OS: Linux
Domoticz version:
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by spacestation »

Hi MikeF,

Pardon my late reply. Firstly thanks for the suggestions and Node-Red codes! I just received my new batch of the Sonoff rf bridges two weeks ago and only managed to try out your suggestions over the weekend. I must say it worked a treat!!!

Currently I have the three 433Mhz wireless touch remotes paired to the rf bridge. The Node-Red code looks something like this -
node_red_resize.jpg
node_red_resize.jpg (129.11 KiB) Viewed 12039 times
I just needed to change the idx in the PreDomoticzCmdx to the appropriate number. It works exactly the same way for controlling groups and scenes just by entering their idx number and changing the payload.command to 'switchscene" .

I was wondering since each wireless remote broadcasts its unique identifier of some kind, is it possible to modify 'ChooseRFKey' to switch between those identifiers instead? In that case is it correct to say that there is no need for the rf bridge to learn the remotes and thus it will not be limited to only 16 remotes?

Once again thanks for the codes! Now room looks much cleaner with less wires running all over the place, the switches can be placed anywhere and they can be easily reconfigured!
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by MikeF »

Glad this is working for you.

Yes, I believe that each wireless remote broadcasts its unique identifier ("Data" in the MQTT payload?), but I just found it easier to code RFKey values in the PrepDomoticzCmd function. Not sure that there is no need for the RF bridge to learn the remotes though - I'm using it with a non-Sonoff remote to control a combination of Sonoff and non-Sonoff devices via Domoticz, and for this, the RF bridge publishes an MQTT topic (domoticz/in) whenever it recognises (learned) buttons pressed on the remote.
peterdr
Posts: 26
Joined: Wednesday 28 November 2018 19:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by peterdr »

Hello i struggle with this a long time.
Had my bridge laying in the corner for some time just because i could not make it work.
I have also a question about mu problem with my bridge in another topic (Just put it there today)
I tried this python script in domoticz but get this error message

2018-11-28 19:53:00.318 Error: EventSystem: Failed to execute python event script "Bridge"
2018-11-28 19:53:00.318 Error: EventSystem: File "<string>", line 24
2018-11-28 19:53:00.318 Error: EventSystem: print "message received " ,str(message.payload.decode("utf-8"))
2018-11-28 19:53:00.318 Error: EventSystem: ^
2018-11-28 19:53:00.318 Error: EventSystem: SyntaxError: invalid syntax

The print commands it gives an error.
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by MikeF »

Are you using Python 3? (I use Python 2.7)
- if so, you need to put brackets around the arguments of print statements, thus:

Code: Select all

print ("message received " ,str(message.payload.decode("utf-8")))
peterdr
Posts: 26
Joined: Wednesday 28 November 2018 19:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by peterdr »

Sorry for my late reply.
The answer to put brackets helps.
I had to do this to all the print messages.
Only it keeping telling me there is no poha module in the import line.
I installed the mqtt clients and also pip install poha-mqtt
But i stil keep this error.
I am not so familiar with this.
The only thing i try to do is put a light on and off with an 433 rf remote button.
The light works already fine with a sonoff touch with tasmote.
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by MikeF »

Are you using python3? If so, type

Code: Select all

python3
Then type

Code: Select all

import paho.mqtt.client
What do you get?

If you are using python3, then you need to change the first line of the python script to this:

Code: Select all

#!/usr/bin/env python3
Incidentally, what hardware / operating system / version of Domoticz are you using?
peterdr
Posts: 26
Joined: Wednesday 28 November 2018 19:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by peterdr »

This i get whwn i type this commands
Attachments
console.jpg
console.jpg (52.78 KiB) Viewed 9847 times
peterdr
Posts: 26
Joined: Wednesday 28 November 2018 19:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by peterdr »

i see that if i only type python it say's me the version 2.7.9
And then import the client and i get the same as my last message
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by MikeF »

If you get an error (not found) message when you type

Code: Select all

python
import paho.mqtt.client
then this means it hasn't been installed under python (2.7.9), only python3.
peterdr
Posts: 26
Joined: Wednesday 28 November 2018 19:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by peterdr »

i don't get an error on both.
not on python and python3
so i asume it should be working.
Only in domoticz it say's in the log no module named paho.
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by MikeF »

Can you post the script you are using here? (use a '</>' block)
peterdr
Posts: 26
Joined: Wednesday 28 November 2018 19:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by peterdr »

It is what you posted here

<#!/usr/bin/python3

'''
Python script to read rfkey number from Sonoff 433MHz RF Bridge
and control corresponding Domoticz.
This example only set up for 2 devices; extend by adding idx's in on_message.
(Not very pretty code!)
'''

import paho.mqtt.client
import time
import json


### substitute your values here ###
broker_address="127.0.0.1" # port = 1883 by default
topic = "<sonoff topic>"
dom_cmd = "http://localhost:8080/json.htm?type=com ... cmd=Toggle"
idx1 = 0
idx2 = 0


###################################

def on_message(client, userdata, message):
print ("message received " ,str(message.payload.decode("utf-8")))
jsonData = json.loads(message.payload)
rfkey = jsonData['RfReceived']['RfKey']
print ("rfkey")
if rfkey == 1:
idx = idx7
elif rfkey == 2:
idx = idx2
else:
return

url = dom_cmd + "&idx=" + str(idx)
print ("url")
r = requests.get(url)

#print "message topic=",message.topic
#print "message qos=",message.qos
#print "message retain flag=",message.retain

print("creating new instance")

client = mqtt.Client("P1") #create new instance
client.on_message=on_message #attach function to callback

print("connecting to broker")
client.connect(broker_address) #connect to broker

client.loop_start() #start the loop

print("Subscribing to topic ") + topic
client.subscribe("tele/" + topic + "/RESULT")

time.sleep(2) # wait
client.loop_forever() #loop forever
time.sleep(2) # wait
client.loop_forever() #loop forever>
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by MikeF »

OK, under the heading '### substitute your values here ###', you need to change the line

Code: Select all

topic = "<sonoff topic>"
Go to the 'Configure MQTT' tab on your RF Bridge, and note the topic value there, and replace the above (including <> brackets).
- e.g., my MQTT topic is sonoffB, so for me this reads

Code: Select all

topic = "sonoffB"
peterdr
Posts: 26
Joined: Wednesday 28 November 2018 19:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by peterdr »

Ok,

Tomorrow i will give it a try.

Thank you.
peterdr
Posts: 26
Joined: Wednesday 28 November 2018 19:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by peterdr »

Ok mi changed it but still get this

2018-12-16 19:22:00.718 Error: EventSystem: Failed to execute python event script "Bridge-test"
2018-12-16 19:22:00.718 Error: EventSystem: Traceback (most recent call last):
2018-12-16 19:22:00.718 Error: EventSystem: File "<string>", line 10, in <module>
2018-12-16 19:22:00.719 Error: EventSystem: ImportError: No module named 'paho'
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: 433Mhz Touch Remote -> Sonoff RF Bridge -> Domoticz

Post by MikeF »

I've noticed a couple more things in your version of the script:
- under '### substitute your values here ###', you have idx1 = 0 and idx2 = 0; you need to replace these with the actual Domoticx idx's of your devices
- and after 'if rfkey == 1:', you should have idx = idx1
- also, the line 'import paho.mqtt.client' should read 'import paho.mqtt.client as mqtt'
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest