Newbie query - How to design flow for RF sensor?

I was advised to ask in this Node Red forum,as I could not design a rule in Openhab for RF sensors that gives out more than just open and close codes.
I have 2 Kerui RF window sensors which gives 4 distinct codes
1)Left Window
48B5EE open
48B5E7 closed
48B5EB tamper
48B5EF low battery

2)Top Window
BDAC1E open
BDAC17 closed
BDAC1B tamper
BDAC1F low battery

I use a sonoff RF bridge to receive the RF signals

How do I design a flow that alerts me when ether of the batteries is low, and if the windows are open between 10pm and 6am?

First, what is the form of the data received from the RF bridge? Is it just a string? Formatted some way? You are using the stock firmware image? Not a customized image on the bridge?

I happen to have a Bond Bridge for control of my fans, which use 303.875 MHz common in US versus 433 MHz is often more common in UK from what I read. But I digress. I ask about the RF bridge, does it have a published API?

Customized firmware for RF bridge you have, I think only supports MQTT communication, not the stock firmware? If the stock firmware publishes status via HTTP/HTTPS REST API or MQTT or something else we can capture via NR, that is the place to start, I would think.

I find several examples where people have changed the bridge firmware to basically hack the bridge to get some type of communication method NR can understand. I would rather not go that route if possible, or at least initially.

In the case of my Bond Bridge, the firm that produces it, publishes a very nice API, with the expectation that it would be leveraged as needed.

I found that there is apparently a published API for your bridge http://developers.sonoff.tech/sonoff-diy-mode-api-protocol.html. Since this API is a REST API, with a bit of work, should be able to get the alert data into NR.

What is your experience with NR?

RF bridge is flashed with Tasmota. data looks like this after opening and closing the windows
"07:43:55 MQT: tele/sonoff/bridge2/RESULT = {"Time":"2020-10-10T07:43:55","RfReceived":{"Sync":9540,"Low":310,"High":940,"Data":"48B5E7","RfKey":"None"}}
07:43:59 MQT: tele/sonoff/bridge2/RESULT = {"Time":"2020-10-10T07:43:59","RfReceived":{"Sync":9480,"Low":310,"High":940,"Data":"48B5E7","RfKey":"None"}}
07:44:01 MQT: tele/sonoff/bridge2/RESULT = {"Time":"2020-10-10T07:44:01","RfReceived":{"Sync":8170,"Low":320,"High":940,"Data":"245AF7","RfKey":"None"}}

regards

Oh, cool. So you do have MQTT communication. Well that makes things a lot easier. So the next question is how do you want the notifications to be done? Email, SMS, popup window on dashboard, something else?

The logical place to start, is to understand how NR handles and process messages, since that is what you are receiving as MQTT communication from your bridge right?

MQTT https://cookbook.nodered.org/mqtt/connect-to-broker
Working with Messages https://nodered.org/docs/user-guide/messages

I would like the notifications sent to me via telegram. This notification seems to works reliably with the other non RF tasmota devices I have.

Yes, what I posted earlier is the usual data sent by the sonoff RF bridge upon opening and closing of the windows

Have you checked what is in the message you are sending to the telegram node?

Frankly, I have not sent any messages of any kind to the RF sensors because I do not know where to start to design the flow. Which is why I am seeking help in this forum.

For a typical tasmota mqtt rule, say for one of my tasmota switches, it will be something like this:

val mqttActions = getActions("mqtt", "mqtt:broker:mosquitto")

rule "OP1 Monitor to OFF"
when
Time cron "0 0 19 1/1 * ? *"
then
mqttActions.publishMQTT("cmnd/sonoff-AEI-op1/POWER", "Off")
val telegramAction = getActions("telegram","telegram:telegramBot:xxxxxxx")
telegramAction.sendTelegram("OP1 Monitor is switched OFF")
logInfo("Info","OP1 Monitor OFF Rule fired")

And the telegram message will come on my phone without issues

I don't recognise the syntax you are using for rules, it doesn't look like anything I have seen in node red.

I think best way for anybody to be able to help you is that we start with the basis. Create a simple flow like below (I guessed the topic, you have to put in the correct topic to pick up the events) with a MQTT in node that connects to your MQTT broker

Open close the windows a couple of times, notice the events coming into Node-RED and show us the results

Once done, we can take it from there

Sorry, this syntax is the rules in Openhab, not in node red.

Something like this?

[{"id":"27d1839a.8c260c","type":"tab","label":"MBR Windows RF Flow","disabled":false,"info":""},{"id":"b7f640b2.ef338","type":"mqtt in","z":"27d1839a.8c260c","name":"","topic":"tele/sonoff/bridge2/RESULT","qos":"2","datatype":"auto","broker":"4606d9fb.a64278","x":142,"y":850,"wires":[["d0f8db9f.685b28"]]},{"id":"d0f8db9f.685b28","type":"json","z":"27d1839a.8c260c","name":"","property":"payload","action":"","pretty":false,"x":308,"y":947,"wires":[["56253c5d.49a5a4"]]},{"id":"56253c5d.49a5a4","type":"function","z":"27d1839a.8c260c","name":"Window Sensors","func":"// Code below is for the rf door sensor at front door\n\nvar on = \"CLOSED\";\nvar off = \"OPEN\";\nvar tamper = \"TAMPER\"\nvar lowbat = \"LOW BATTERY\"\n\nmsg1={};\n\n// Left Window Sensor\nif (msg.payload.RfReceived.Data==\"48B5E7\"){\n  msg1.payload = on;\n   return [msg1,null,null,null]}\n else if (msg.payload.RfReceived.Data==\"48B5EE\"){\n  msg1.payload = off;\n   return [msg1,null,null,null]}\n else if (msg.payload.RfReceived.Data==\"48B5EB\"){\n  msg1.payload = tamper;\n   return [msg1,null,null,null]}\n else if (msg.payload.RfReceived.Data==\"48B5EF\"){\n  msg1.payload = lowbat;\n   return [msg1,null,null,null]}  \n// Middle Top Window Sensor\nelse if (msg.payload.RfReceived.Data==\"BDAC17\"){\n  msg1.payload = on;\n   return [null,msg1,null,null]}\n else if (msg.payload.RfReceived.Data==\"BDAC1E\"){\n  msg1.payload = off;\n   return [null,msg1,null,null]}\n else if (msg.payload.RfReceived.Data==\"48B5EB\"){\n  msg1.payload = tamper;\n   return [msg1,null,null,null]}\n else if (msg.payload.RfReceived.Data==\"48B5EF\"){\n  msg1.payload = lowbat;\n   return [msg1,null,null,null]}  \n","outputs":2,"noerr":0,"x":523,"y":976,"wires":[["bb60227b.bfad6"],["37fe65b6.3e1bca"]]},{"id":"bb60227b.bfad6","type":"mqtt out","z":"27d1839a.8c260c","name":"Left  Window","topic":"tele/sonoff/bridge2/RESULT","qos":"","retain":"true","broker":"4606d9fb.a64278","x":815,"y":782,"wires":[]},{"id":"37fe65b6.3e1bca","type":"mqtt out","z":"27d1839a.8c260c","name":"Middle Top Window","topic":"tele/sonoff/bridge2/RESULT","qos":"","retain":"true","broker":"4606d9fb.a64278","x":804,"y":970,"wires":[]},{"id":"4606d9fb.a64278","type":"mqtt-broker","z":"","name":"mosquitto","broker":"192.168.0.150","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"1","birthRetain":"true","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Well, in that flow you are just sending the messages for each window back to the MQTT broker, I guess that is not what you want to do?

I assume you receive correct messages now for each window?? Did you check with a debug node?

Let's start with some thoughts about your requirements

When a window is opening/closing, the tamper alarm goes on or the battery is low, I assume you will get a message only when they happens? Best is to store current states for the opening/closing since you will need to keep that when you evaluate if you shall send an alert or not. If the window is opened outside 10pm and 6am and kept open, you want to get informed "later" when the time is within the valid range. The other messages for tamper and battery will be sent immediately around the clock. So since you seem to feel pretty familiar with function nodes and javascript, I have made an example below with function nodes and code. There are many other node types available that could be used instead like gates, cron+, switch, change etc etc. Plenty of room for alternatives....

The rbe nodes in the flow is to prevent repeated messages during 10pm to 6am :wink:

[{"id":"b7f640b2.ef338","type":"mqtt in","z":"27d1839a.8c260c","name":"","topic":"tele/sonoff/bridge2/RESULT","qos":"2","datatype":"auto","broker":"4606d9fb.a64278","x":170,"y":140,"wires":[["d0f8db9f.685b28"]]},{"id":"d0f8db9f.685b28","type":"json","z":"27d1839a.8c260c","name":"","property":"payload","action":"","pretty":false,"x":380,"y":140,"wires":[["56253c5d.49a5a4"]]},{"id":"56253c5d.49a5a4","type":"function","z":"27d1839a.8c260c","name":"Window Sensors","func":"// Code below is for the rf door sensor at front door\n\n\n// Left Window Sensor\nif (msg.payload.RfReceived.Data==\"48B5E7\"){\n    flow.set('Left_Window_Sensor',\"CLOSED\")\n}\nif (msg.payload.RfReceived.Data==\"48B5EE\"){\n    flow.set('Left_Window_Sensor',\"OPEN\")\n}\nif (msg.payload.RfReceived.Data==\"48B5EB\"){\n    return [{payload:\"TAMPER\"},null,null,null]}\nif (msg.payload.RfReceived.Data==\"48B5EF\"){\n    return [{payload:\"LOW BATTERY\"},null,null,null]}  \n\n// Middle Top Window Sensor\nif (msg.payload.RfReceived.Data==\"BDAC17\"){\n    flow.set('Middle_Top_Window',\"CLOSED\")\n}\nif (msg.payload.RfReceived.Data==\"BDAC1E\"){\n    flow.set('Middle_Top_Window',\"OPEN\")\n}\nif (msg.payload.RfReceived.Data==\"BDAC1B\"){\n    return [null,{payload:\"TAMPER\"},null,null]}\nif (msg.payload.RfReceived.Data==\"BDAC1F\"){\n    return [null,{payload:\"LOW BATTERY\"},null,null]}  \n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":600,"y":140,"wires":[["7815fd50.235484"],["4bdce9ca.39ead8"]]},{"id":"d20c6df8.53f2c","type":"inject","z":"27d1839a.8c260c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":290,"wires":[["3ef83812.dd2f68"]]},{"id":"3ef83812.dd2f68","type":"function","z":"27d1839a.8c260c","name":"h >= 22 || h < 6","func":"var h = new Date(msg.payload).getHours();\n//node.warn(h);\n\nif (h >= 22 || h < 6){\n    if (flow.get('Left_Window_Sensor') == \"OPEN\"){\n        node.send([{payload:\"Left Window OPEN\"},null]);\n    }\n    else{\n        node.send([{payload:\"Left Window CLOSED\"},null]);\n    }\n    if (flow.get('Middle_Top_Window') == \"OPEN\"){\n        node.send([null,{payload:\"Middle Top Window OPEN\"}]);\n    }\n    else{\n        node.send([null,{payload:\"Middle Top Window CLOSED\"}]);\n    }\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":350,"y":290,"wires":[["8103e63f.f50498"],["3ac173b2.049a1c"]]},{"id":"8d38725.02ed89","type":"debug","z":"27d1839a.8c260c","name":"Left Window open alerts","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":260,"wires":[]},{"id":"7815fd50.235484","type":"debug","z":"27d1839a.8c260c","name":"Left window","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":100,"wires":[]},{"id":"4bdce9ca.39ead8","type":"debug","z":"27d1839a.8c260c","name":"Middle top window","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":180,"wires":[]},{"id":"8cb6cef1.ea121","type":"comment","z":"27d1839a.8c260c","name":"Tamper & Battery alerts","info":"","x":860,"y":60,"wires":[]},{"id":"a090f6e.b37c408","type":"comment","z":"27d1839a.8c260c","name":"Tamper & Battery alerts","info":"","x":860,"y":140,"wires":[]},{"id":"8103e63f.f50498","type":"rbe","z":"27d1839a.8c260c","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":560,"y":260,"wires":[["8d38725.02ed89"]]},{"id":"3fba7250.7ba2ce","type":"debug","z":"27d1839a.8c260c","name":"Middle Top Window open alerts","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":320,"wires":[]},{"id":"3ac173b2.049a1c","type":"rbe","z":"27d1839a.8c260c","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":560,"y":320,"wires":[["3fba7250.7ba2ce"]]},{"id":"6c7cbb6a.1c5654","type":"inject","z":"27d1839a.8c260c","name":"48B5EE","props":[{"p":"payload.RfReceived.Data","v":"48B5EE","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":380,"y":40,"wires":[["56253c5d.49a5a4"]]},{"id":"36e5b508.b3096a","type":"inject","z":"27d1839a.8c260c","name":"48B5E7","props":[{"p":"payload.RfReceived.Data","v":"48B5E7","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":380,"y":80,"wires":[["56253c5d.49a5a4"]]},{"id":"4606d9fb.a64278","type":"mqtt-broker","z":"","name":"mosquitto","broker":"192.168.0.150","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"1","birthRetain":"true","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
1 Like

Here's a write up I did recently regarding working with Telegram notifications. I'm taking sensor readings from Home-Assistant into Node-red, it should be pretty easy for you to swap in your MQTT payload. It might be of interest....

Thanks so much!
Actually I am unfamiliar with anything node red . But I am trying to learn and understand the logic. Let me try to digest what you just shared.

regards

In that case please spend an hour or two watching the excellent Node Red Essentials videos linked from the node red docs pages. It will save much time in the long run.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.