Hi all,
I’m in the process of moving my automations from Home Assistant Automations to Node-RED and I’m working on one that checks whether specific conditions are met to determine if someone is sleeping.
The conditions I’m checking are:
• Phone is at home
• Phone is charging
• Phone is on silent
If all three conditions are true, it indicates the person is sleeping. I’m doing this for two phones (Mr and Mrs), and I want the automation to have three possible outputs:
- Both people are sleeping
- One person is sleeping
- Neither is sleeping
However, I’m running into an issue where the switch isn’t giving any output. Below is the flow structure I’ve set up and the exported code.
Could anyone help identify why the switch isn’t providing any output and how I can fix the node configurations? Also, is there any way to streamline this flow to make it more efficient?
Thanks in advance for your help!
[{"id":"ba9fdde779b44998","type":"tab","label":"Flow 5","disabled":false,"info":"","env":[]},{"id":"31f9a5de436dff62","type":"junction","z":"ba9fdde779b44998","x":320,"y":260,"wires":[["908a188c753387a5","ec9e9465c28b7a10","a0b3d83ca59994bd","5d95a570e4019349","7f9546ca7fdae603","9edc8523b3881144"]]},{"id":"daa4356a5afe015b","type":"junction","z":"ba9fdde779b44998","x":940,"y":260,"wires":[["df334ef1aadba2f3","e2304e9da0d57e3b"]]},{"id":"908a188c753387a5","type":"api-current-state","z":"ba9fdde779b44998","name":"Is the Mr at Home","server":"2690468.2ea7fba","version":3,"outputs":2,"halt_if":"home","halt_if_type":"str","halt_if_compare":"is","entity_id":"person.rpc","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"home","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":80,"wires":[["b32ea532f98a260d","1c41698ff379104a"],[]]},{"id":"ec9e9465c28b7a10","type":"api-current-state","z":"ba9fdde779b44998","name":"Is the Mr charging ","server":"2690468.2ea7fba","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.the_mr_is_charging","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"charging","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":140,"wires":[["7c1ece4d1b470355","1c41698ff379104a"],[]]},{"id":"2bd2a6b3a782be96","type":"function","z":"ba9fdde779b44998","name":"Is the Mr Sleeping","func":"// Ensure the joined payload contains the correct topics\nlet atHomeState = msg.payload.home || \"\"; // Access 'home' value\nlet chargingState = msg.payload.charging || \"\"; // Access 'charging' value\nlet silentState = msg.payload.silent || \"\"; // Access 'silent' value\n\n// Log the cleaned-up values for verification\nnode.warn(\"atHomeState: \" + atHomeState);\nnode.warn(\"chargingState: \" + chargingState);\nnode.warn(\"silentState: \" + silentState);\n\n// Check if all three conditions are met\nif (atHomeState === \"home\" && chargingState === \"on\" && silentState === \"silent\") {\n msg.payload = { \"Mr\": \"true\" }; // Mr is sleeping\n} else {\n msg.payload = { \"Mr\": \"false\" }; // Mr is not sleeping\n}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":850,"y":140,"wires":[["d0a1522378dd12d4","daa4356a5afe015b"]]},{"id":"b32ea532f98a260d","type":"debug","z":"ba9fdde779b44998","name":"Mr Home","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":60,"wires":[]},{"id":"7c1ece4d1b470355","type":"debug","z":"ba9fdde779b44998","name":"Mr Charging","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":100,"wires":[]},{"id":"d0a1522378dd12d4","type":"debug","z":"ba9fdde779b44998","name":"Mr is sleeping","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":80,"wires":[]},{"id":"1c41698ff379104a","type":"join","z":"ba9fdde779b44998","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":false,"accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":670,"y":140,"wires":[["2bd2a6b3a782be96","dde38d4141093b53"]]},{"id":"dde38d4141093b53","type":"debug","z":"ba9fdde779b44998","name":"Mr join debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":840,"y":180,"wires":[]},{"id":"a0b3d83ca59994bd","type":"api-current-state","z":"ba9fdde779b44998","name":"Is the Mr on Silent","server":"2690468.2ea7fba","version":3,"outputs":2,"halt_if":"silent","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.the_mr_ringer_mode","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"silent","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":200,"wires":[["c6b60d5a7bc9e764","1c41698ff379104a"],[]]},{"id":"c6b60d5a7bc9e764","type":"debug","z":"ba9fdde779b44998","name":"Mr Silent","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":220,"wires":[]},{"id":"7f9546ca7fdae603","type":"api-current-state","z":"ba9fdde779b44998","name":"Is the Mrs charging ","server":"2690468.2ea7fba","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary_sensor.the_mrs_is_charging","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"charging","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":450,"y":400,"wires":[["087c6389e0419ed7","3b8ed8169cb3b87a"],[]]},{"id":"5d95a570e4019349","type":"api-current-state","z":"ba9fdde779b44998","name":"Is the Mrs at Home","server":"2690468.2ea7fba","version":3,"outputs":2,"halt_if":"home","halt_if_type":"str","halt_if_compare":"is","entity_id":"person.the_mrs","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"home","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":450,"y":340,"wires":[["206ce60acb5c6983","087c6389e0419ed7"],[]]},{"id":"a86048a7ae55c5bc","type":"function","z":"ba9fdde779b44998","name":"Is the Mrs Sleeping","func":"// Ensure the joined payload contains the correct topics\nlet atHomeState = msg.payload.home || \"\"; // Access 'home' value\nlet chargingState = msg.payload.charging || \"\"; // Access 'charging' value\nlet silentState = msg.payload.silent || \"\"; // Access 'silent' value\n\n// Log the cleaned-up values for verification\nnode.warn(\"atHomeState: \" + atHomeState);\nnode.warn(\"chargingState: \" + chargingState);\nnode.warn(\"silentState: \" + silentState);\n\n// Check if all three conditions are met\nif (atHomeState === \"home\" && chargingState === \"on\" && silentState === \"silent\") {\n msg.payload = { \"Mrs\": \"true\" }; // Mrs is sleeping\n} else {\n msg.payload = { \"Mrs\": \"false\" }; // Mrs is not sleeping\n}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":870,"y":360,"wires":[["cefbfa6cf1a67c93","daa4356a5afe015b"]]},{"id":"206ce60acb5c6983","type":"debug","z":"ba9fdde779b44998","name":"Mrs Home","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":280,"wires":[]},{"id":"3b8ed8169cb3b87a","type":"debug","z":"ba9fdde779b44998","name":"Mrs Charge","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":320,"wires":[]},{"id":"cefbfa6cf1a67c93","type":"debug","z":"ba9fdde779b44998","name":"Mrs is sleeping","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":420,"wires":[]},{"id":"087c6389e0419ed7","type":"join","z":"ba9fdde779b44998","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":false,"accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":670,"y":380,"wires":[["a86048a7ae55c5bc","be19e113bd84558b"]]},{"id":"be19e113bd84558b","type":"debug","z":"ba9fdde779b44998","name":"Mrs join debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":460,"wires":[]},{"id":"9edc8523b3881144","type":"api-current-state","z":"ba9fdde779b44998","name":"Is the Mrs on Silent","server":"2690468.2ea7fba","version":3,"outputs":2,"halt_if":"silent","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.the_mrs_ringer_mode","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"silent","valueType":"str"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":450,"y":460,"wires":[["9bd63c3521cf8df7","087c6389e0419ed7"],[]]},{"id":"9bd63c3521cf8df7","type":"debug","z":"ba9fdde779b44998","name":"Mrs Silent","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":440,"wires":[]},{"id":"8b478b3fbcf39552","type":"inject","z":"ba9fdde779b44998","name":"Start","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":310,"y":20,"wires":[["31f9a5de436dff62"]]},{"id":"e2304e9da0d57e3b","type":"debug","z":"ba9fdde779b44998","name":"debug before switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1140,"y":100,"wires":[]},{"id":"df334ef1aadba2f3","type":"switch","z":"ba9fdde779b44998","name":"Anyone sleeping","property":"msg.payload","propertyType":"jsonata","rules":[{"t":"jsonata_exp","v":"payload.Mr = \"true\" and payload.Mrs = \"true\"","vt":"jsonata"},{"t":"jsonata_exp","v":"(payload.Mr = \"true\" and payload.Mrs = \"false\") or (payload.Mr = \"false\" and payload.Mrs = \"true\")","vt":"jsonata"},{"t":"jsonata_exp","v":"payload.Mr = \"false\" and payload.Mrs = \"false\"","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":3,"x":1080,"y":260,"wires":[["e2b8e4e411f6d0e6"],["ce35d50d91ef2ef0"],["8b34c2af1e7ce2e1"]],"outputLabels":["Both are sleeping","One sleeping, other not","Neither is sleeping"]},{"id":"e2b8e4e411f6d0e6","type":"debug","z":"ba9fdde779b44998","name":"Both are sleeping","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":200,"wires":[]},{"id":"ce35d50d91ef2ef0","type":"debug","z":"ba9fdde779b44998","name":"One sleeping, other not","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1290,"y":260,"wires":[]},{"id":"8b34c2af1e7ce2e1","type":"debug","z":"ba9fdde779b44998","name":"Neither is sleeping","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1150,"y":320,"wires":[]},{"id":"2690468.2ea7fba","type":"server","name":"Home Assistant","addon":true}]
Thanks in advance for your help!