Translating Flow - Sonoff RF Bridge

Hey there,

I just started learning NodeRed after 1 year of using HA. I moved my automations over to Node Red and exploring the possibilities.

I have this motion sensor flow that lights the room after sunset with movement. The MQTT PIR sensor is configured in HA.

[{"id":"25dc27c0.8e93d8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"51da6d59.8dcac4","type":"server-state-changed","z":"25dc27c0.8e93d8","name":"Motion: Living RM","server":"a71c8062.e923a","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.living_room_motion","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":170,"y":140,"wires":[["91746bcb.52c208"]]},{"id":"91746bcb.52c208","type":"switch","z":"25dc27c0.8e93d8","name":"on or off ?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":380,"y":140,"wires":[["ed7d63d6.4a03e","a204198e.f5b148"],["a204198e.f5b148"]]},{"id":"f43b9a69.37bd78","type":"api-call-service","z":"25dc27c0.8e93d8","name":"Motion On Light","server":"a71c8062.e923a","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.living_room","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":940,"y":80,"wires":[[]]},{"id":"a204198e.f5b148","type":"trigger","z":"25dc27c0.8e93d8","op1":"","op2":"off","op1type":"nul","op2type":"str","duration":"15","extend":false,"units":"s","reset":"on","bytopic":"all","name":"Remain on Till Motion","x":680,"y":180,"wires":[["a462b9e5.15bfa8"]]},{"id":"a462b9e5.15bfa8","type":"api-call-service","z":"25dc27c0.8e93d8","name":"Turn Living Off","server":"a71c8062.e923a","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.living_room","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":940,"y":180,"wires":[[]]},{"id":"ed7d63d6.4a03e","type":"api-current-state","z":"25dc27c0.8e93d8","name":"Sunset","server":"a71c8062.e923a","version":1,"outputs":2,"halt_if":"below_horizon","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sun.sun","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":640,"y":80,"wires":[["f43b9a69.37bd78"],[]]},{"id":"a71c8062.e923a","type":"server","z":"","name":"Home Assistant","addon":true}]

I am trying to shift to MQTT in NodeRed for its advantages and to have HA as state machine only.

[{"id":"fd7002c2.58a1d","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"10b9bfdf.498c9","type":"mqtt in","z":"fd7002c2.58a1d","name":"","topic":"stat/sonoff/POWER","qos":"1","datatype":"auto","broker":"16eb98f8.aeddc7","x":230,"y":160,"wires":[["6c9bdad1.e386e4"]]},{"id":"6c9bdad1.e386e4","type":"ui_text","z":"fd7002c2.58a1d","group":"71344126.f46a5","order":0,"width":0,"height":0,"name":"","label":"mqtt","format":"{{msg.payload}}","layout":"row-spread","x":450,"y":160,"wires":[]},{"id":"b172a265.14724","type":"mqtt in","z":"fd7002c2.58a1d","name":"","topic":"tele/sonoff-BR/RESULT","qos":"1","datatype":"auto","broker":"16eb98f8.aeddc7","x":200,"y":460,"wires":[["9b438da3.b0ebf","289f6596.36e3aa"]]},{"id":"289f6596.36e3aa","type":"json","z":"fd7002c2.58a1d","name":"","property":"payload","action":"obj","pretty":false,"x":480,"y":480,"wires":[["1a8190ff.5c52cf","f0cec590.ae2e88"]]},{"id":"1a8190ff.5c52cf","type":"switch","z":"fd7002c2.58a1d","name":"","property":"payload.RfReceived.Data","propertyType":"msg","rules":[{"t":"eq","v":"EF7E5E","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":650,"y":480,"wires":[["12ad76d6.3fe6a9","6cd7a285.ceed7c"]]},{"id":"f871f0f4.4ec12","type":"api-call-service","z":"fd7002c2.58a1d","name":"Motion On Light","server":"a71c8062.e923a","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.sarvesh_room","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1080,"y":420,"wires":[[]]},{"id":"12ad76d6.3fe6a9","type":"change","z":"fd7002c2.58a1d","name":"Set ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":440,"wires":[["f871f0f4.4ec12"]]},{"id":"5563055.0d759fc","type":"api-call-service","z":"fd7002c2.58a1d","name":"Motion Off Light","server":"a71c8062.e923a","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.sarvesh_room","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1080,"y":520,"wires":[[]]},{"id":"6cd7a285.ceed7c","type":"change","z":"fd7002c2.58a1d","name":"Set OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":500,"wires":[["5563055.0d759fc"]]},{"id":"9b438da3.b0ebf","type":"debug","z":"fd7002c2.58a1d","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":370,"y":600,"wires":[]},{"id":"f0cec590.ae2e88","type":"debug","z":"fd7002c2.58a1d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":620,"wires":[]},{"id":"16eb98f8.aeddc7","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"71344126.f46a5","type":"ui_group","z":"","name":"Default","tab":"2b63f3e3.c7da9c","disp":true,"width":"6","collapse":false},{"id":"a71c8062.e923a","type":"server","z":"","name":"Home Assistant","addon":true},{"id":"2b63f3e3.c7da9c","type":"ui_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

I can receive the RF code and i break it in the JSON as object.

{"Time":"2020-05-09T21:44:19","RfReceived":{"Sync":12510,"Low":450,"High":1230,"Data":"EF7E5E","RfKey":"None"}}

From there on, i can’t figure out how to proceed. In HA, we had the on code and a false off code since its a PIR one code sensor. Also, how to let HA knows of the state?

my HA config:

# MQTT Binary Sensors
binary_sensor:
  - platform: mqtt
    state_topic: "tele/sonoff-BR/RESULT"
    name: 'Living Room Motion'
    value_template: '{{value_json.RfReceived.Data}}'
    payload_on: 'EF7E5E'
    payload_off: 'EF7E5Eoff'
    device_class: motion
#    optimistic: false
    qos: 1
#    retain: false
    off_delay: 5

just change the switch node little bit so if the message is something other than ‘EF7E5E’, it can go to ‘off’ route.

[{"id":"91bc2227.49754","type":"mqtt in","z":"2c7b060a.00328a","name":"","topic":"tele/sonoff-BR/RESULT","qos":"1","datatype":"auto","broker":"8d91578d.a27f18","x":200,"y":460,"wires":[["4d1dae78.bf542","3ac5b3c7.33181c"]]},{"id":"3ac5b3c7.33181c","type":"json","z":"2c7b060a.00328a","name":"","property":"payload","action":"obj","pretty":false,"x":480,"y":480,"wires":[["cc9b10cf.f90e2","339a3c14.2981f4"]]},{"id":"cc9b10cf.f90e2","type":"switch","z":"2c7b060a.00328a","name":"","property":"payload.RfReceived.Data","propertyType":"msg","rules":[{"t":"eq","v":"EF7E5E","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":480,"wires":[["cc450aee.a93ff8"],["7729017b.465aa"]]},{"id":"77e48503.fcd59c","type":"api-call-service","z":"2c7b060a.00328a","name":"Motion On Light","server":"bd7636e9.005218","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.sarvesh_room","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1080,"y":420,"wires":[[]]},{"id":"cc450aee.a93ff8","type":"change","z":"2c7b060a.00328a","name":"Set ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":440,"wires":[["77e48503.fcd59c"]]},{"id":"632af7c.20c8308","type":"api-call-service","z":"2c7b060a.00328a","name":"Motion Off Light","server":"bd7636e9.005218","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.sarvesh_room","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1080,"y":520,"wires":[[]]},{"id":"7729017b.465aa","type":"change","z":"2c7b060a.00328a","name":"Set OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":500,"wires":[["632af7c.20c8308"]]},{"id":"4d1dae78.bf542","type":"debug","z":"2c7b060a.00328a","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":370,"y":600,"wires":[]},{"id":"339a3c14.2981f4","type":"debug","z":"2c7b060a.00328a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":620,"wires":[]},{"id":"8d91578d.a27f18","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"bd7636e9.005218","type":"server","z":"","name":"Home Assistant","addon":true}]

image

That is the approach I use but then, this is a Sonoff RF Bridge and other devices have other codes. If i put Otherwise as OFF, then if i press a RF switch button giving another code, it will turn off this light.

This is the code when PIR becomes off? EF7E5Eoff
Then change the second output in the switch as == EF7E5Eoff

The PIR generates only EF7E5E when motion is detected, the EF7E5Eoff is not sent on the RF Bridge. The payload_off was just an implementation via Home Assistant.

Ok so when the motion detected, it’s sends a message and nothing happens when there is no movement. I think ‘wait until’ node can help here. I am on mobile so can’t test it but try putting node, give it 1 second for cool off, if no movement in 1 sec, send off payload.

Edit: Trigger node after the switch node will be more appropriate: