there is no code, just a group:
wc_invitados:
name: WC invitados
entities:
- binary_sensor.motion_2_occupancy
- light.shellybulbduo_8xxxxxxx
Then just a node-red flow that is turning the lights on on motion detection, then turning it off on no more motion detected. Although it never worked properly (I wanted the detection of motion to reset the countdown to turn lights off, but it never worked properly)
[{"id":"5a394d0a.224e54","type":"subflow","name":"Auto off light","info":"You provide a light id and it will get turned on on any input.\nAn off delay will be triggered at the same time, and it will turn off the light after a minute","category":"","in":[{"x":50,"y":30,"wires":[{"id":"a7d14362.55862"}]}],"out":[],"env":[{"name":"light","type":"str","value":"","ui":{"icon":"font-awesome/fa-lightbulb-o","type":"input","opts":{"types":["str","env"]}}},{"name":"sensor","type":"str","value":""}],"color":"#FFCC66","icon":"font-awesome/fa-lightbulb-o","status":{"x":580,"y":360,"wires":[{"id":"eca7a043.764f5","port":0}]}},{"id":"3a774b11.81cc34","type":"api-call-service","z":"5a394d0a.224e54","name":"Turn on light","server":"244e8d39.7b4e02","version":3,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"{{payload}}","data":"{}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":590,"y":60,"wires":[[]]},{"id":"2cb6384a.333aa8","type":"api-call-service","z":"5a394d0a.224e54","name":"Turn off light","server":"244e8d39.7b4e02","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"{{payload}}","data":"{}","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":590,"y":220,"wires":[[]]},{"id":"8809cd64.f43ea","type":"trigger","z":"5a394d0a.224e54","name":"Off delay","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"10","extend":true,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":580,"y":160,"wires":[["2cb6384a.333aa8"]]},{"id":"d28a794d.850b68","type":"debug","z":"5a394d0a.224e54","name":"Auto-off-light","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":100,"wires":[]},{"id":"eca7a043.764f5","type":"status","z":"5a394d0a.224e54","name":"","scope":null,"x":420,"y":360,"wires":[[]]},{"id":"a7d14362.55862","type":"function","z":"5a394d0a.224e54","name":"Split states","func":"const id = env.get('light')\nif(!id) {\n node.status({\n fill:\"red\",\n shape:\"ring\",\n text:\"light not provided\"\n });\n return null\n}\n\nnode.log(JSON.stringify(msg))\nmsg.payload = id // Required to turn on/off the proper light\n// Sent by homeassistant api-current-state\nconst isOn = msg.data.new_state.state === 'on'\nif(isOn) return [msg,null];\nreturn [null, msg]","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":100,"wires":[["3a774b11.81cc34","d28a794d.850b68"],["8809cd64.f43ea","d28a794d.850b68"]],"inputLabels":["Ha state"],"outputLabels":["on","off"]},{"id":"76137ab1.8121d4","type":"server-state-changed","z":"d326cf3e.06bee","name":"WC-motion","server":"244e8d39.7b4e02","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"group.wc_invitados","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":90,"y":240,"wires":[["18326890.31eca7"]]},{"id":"18326890.31eca7","type":"subflow:5a394d0a.224e54","z":"d326cf3e.06bee","name":"WC auto-off","env":[{"name":"light","value":"group.wc_invitados","type":"str"},{"name":"wait","value":"61","type":"num"}],"x":290,"y":240,"wires":[]},{"id":"244e8d39.7b4e02","type":"server","name":"Home Assistant","version":1,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
The motion sensor is aqara and the light is a shelly one