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

Post Reply
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 » Tuesday 07 August 2018 19:56

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: 227
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

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

Post by MikeF » Tuesday 14 August 2018 16:08

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: 227
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

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

Post by MikeF » Wednesday 15 August 2018 9:18

... 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: 227
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

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

Post by MikeF » Sunday 02 September 2018 8:46

@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 » Wednesday 05 September 2018 5:41

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 1392 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: 227
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi
Domoticz version: V4.9700
Location: UK
Contact:

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

Post by MikeF » Wednesday 05 September 2018 11:47

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest