Node-red. How would you program a bathroom contact light sensor

That looks nice! I've been messing around with finite state machines lately. Incorporating that concept the logic could be redone like this:

Finite State Machine Example

[{"id":"ba7e0603.6b0938","type":"inject","z":"af91369d.586338","name":"Motion = active","props":[{"p":"payload.value","v":"active","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":380,"y":460,"wires":[["6f7022d6.e3490c"]]},{"id":"b21ec898.15e9b8","type":"inject","z":"af91369d.586338","name":"Motion = inactive","props":[{"p":"payload.value","v":"inactive","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":380,"y":500,"wires":[["6f7022d6.e3490c"]]},{"id":"a50dd8b6.2561c8","type":"inject","z":"af91369d.586338","name":"Contact = open","props":[{"p":"payload.value","v":"open","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":380,"y":600,"wires":[["6f7022d6.e3490c"]]},{"id":"f649bf76.1cd48","type":"inject","z":"af91369d.586338","name":"Contact = closed","props":[{"p":"payload.value","v":"closed","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":380,"y":640,"wires":[["6f7022d6.e3490c"]]},{"id":"64ad2c72.b10f14","type":"debug","z":"af91369d.586338","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1470,"y":540,"wires":[]},{"id":"5a1f77aa.72cd88","type":"finite-state-machine","z":"af91369d.586338","name":"","fsmDefinition":"{\"state\":{\"status\":\"CLOSED/INACTIVE\"},\"transitions\":{\"CLOSED/INACTIVE\":{\"active\":\"CLOSED/ACTIVE\",\"open\":\"OPEN/INACTIVE\"},\"OPEN/INACTIVE\":{\"active\":\"OPEN/ACTIVE\",\"closed\":\"CLOSED/INACTIVE\"},\"CLOSED/ACTIVE\":{\"inactive\":\"CLOSED/INACTIVE\",\"open\":\"OPEN/ACTIVE\"},\"OPEN/ACTIVE\":{\"inactive\":\"OPEN/INACTIVE\",\"closed\":\"CLOSED/ACTIVE\"}}}","sendInitialState":false,"sendStateWithoutChange":false,"showTransitionErrors":false,"x":890,"y":540,"wires":[["84886862.faf398"]]},{"id":"6f7022d6.e3490c","type":"change","z":"af91369d.586338","name":"topic = payload.value","rules":[{"t":"set","p":"topic","pt":"msg","to":"payload.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":540,"wires":[["5a1f77aa.72cd88"]]},{"id":"84886862.faf398","type":"switch","z":"af91369d.586338","name":"CLOSED/INACTIVE \\n OPEN/INACTIVE \\n CLOSED/ACTIVE \\n OPEN/ACTIVE","property":"payload.status","propertyType":"msg","rules":[{"t":"eq","v":"CLOSED/INACTIVE","vt":"str"},{"t":"eq","v":"OPEN/INACTIVE","vt":"str"},{"t":"eq","v":"CLOSED/ACTIVE","vt":"str"},{"t":"eq","v":"OPEN/ACTIVE","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":1140,"y":540,"wires":[["64ad2c72.b10f14"],["64ad2c72.b10f14"],["64ad2c72.b10f14"],["64ad2c72.b10f14"]]}]

Note: you need the node-red-contrib-finite-statemachine node. I don't know if it is really any more efficient though.. but since I now have an FSM "hammer" in my NR "toolbox" everything looks like a nail... :laughing:

On a side note - recommend when posting a flow/sequence use three [` ] at the beginning and end of the code that way it's easier for people to cut and paste...

<details><summary>Posting Code Example</summary>
<p>

```
[your cool stuff here]
```
</p>
</details>

EDIT: I've likely missed a few state transitions but this should at least give you an idea of what I'm babbling about. I am currently interested in exploring these kinds of device interactions / aggregations in NR so thank you for posting your solution!

1 Like