Set brightness on motion based on current time

I want to set the brightness of the toilet light, based on the current time. I think the problem is in the switch-node, because when I cut out the switch-node, everything works just fine.

I could not find any related information topics. Who has a solution for my problem?

[{"id":"cca730ed.d6e4d","type":"comment","z":"393a4b6.bdd58b4","name":"Motion activated toilet light","info":"","x":150,"y":95,"wires":[]},{"id":"8ef37aa0.d8399","type":"server-state-changed","z":"393a4b6.bdd58b4","name":"Motion toilet","server":"18501424.c3841c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_v2_sensor_3","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":110,"y":140,"wires":[["18ed3339.0072dd"]]},{"id":"502738d4.8fd8e","type":"api-call-service","z":"393a4b6.bdd58b4","name":"Toilet on","server":"18501424.c3841c","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.08067822c44f3392f7db","data":"{\"brightness_pct\":\"100\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":680,"y":100,"wires":[["843369d7.a83618"]]},{"id":"843369d7.a83618","type":"stoptimer","z":"393a4b6.bdd58b4","duration":"2","units":"Minute","payloadtype":"num","payloadval":"0","name":"2 minuten wachten","x":1120,"y":155,"wires":[["dc5c7bb1.a29e8"],[]]},{"id":"dc5c7bb1.a29e8","type":"api-current-state","z":"393a4b6.bdd58b4","name":"Beweging toilet?","server":"18501424.c3841c","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"binary_sensor.shenzhen_neo_electronics_co_ltd_battery_powered_pir_sensor_v2_sensor_3","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1210,"y":215,"wires":[["236edeca.cf3512"],["43d41b43.a12d14"]]},{"id":"236edeca.cf3512","type":"stoptimer","z":"393a4b6.bdd58b4","duration":"30","units":"Second","payloadtype":"num","payloadval":"0","name":"30 seconden wachten","x":1420,"y":155,"wires":[["dc5c7bb1.a29e8"],[]]},{"id":"43d41b43.a12d14","type":"api-call-service","z":"393a4b6.bdd58b4","name":"Toilet off","server":"18501424.c3841c","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.08067822c44f3392f7db","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1420,"y":215,"wires":[[]]},{"id":"c0e698d8.fbe42","type":"switch","z":"393a4b6.bdd58b4","name":"Switch time","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"06:30","vt":"num","v2":"22:15","v2t":"num"},{"t":"btwn","v":"22:16","vt":"num","v2":"06:29","v2t":"num"}],"checkall":"true","repair":false,"outputs":2,"x":490,"y":140,"wires":[["502738d4.8fd8e","628da254.9b1b1c"],["63da43ab.f2889c","628da254.9b1b1c"]]},{"id":"18ed3339.0072dd","type":"api-current-state","z":"393a4b6.bdd58b4","name":"What time is it?","server":"18501424.c3841c","version":1,"outputs":1,"halt_if":"","halt_if_type":"num","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.time","state_type":"str","state_location":"","override_payload":"none","entity_location":"","override_data":"none","blockInputOverrides":false,"x":380,"y":60,"wires":[["c0e698d8.fbe42"]]},{"id":"63da43ab.f2889c","type":"api-call-service","z":"393a4b6.bdd58b4","name":"Toilet on","server":"18501424.c3841c","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.08067822c44f3392f7db","data":"{\"brightness_pct\":\"30\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":680,"y":160,"wires":[["843369d7.a83618"]]},{"id":"628da254.9b1b1c","type":"debug","z":"393a4b6.bdd58b4","name":"Output tijd","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":280,"wires":[]},{"id":"3c31bee3.bb59c2","type":"inject","z":"393a4b6.bdd58b4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"msg","x":130,"y":260,"wires":[["18ed3339.0072dd"]]},{"id":"18501424.c3841c","type":"server","z":"","name":"Home Assistant","addon":true}]

Ehm, what’s the problem?

I think you are missing the logic. The payload from the motion toilet is going into the switchnode. I don’t think the time is going into it properly. Have you used the debug Node?

I can’t currently see the name of the node that passes or blocks your payload from the motion toilet Node. But if you use that, based on the time it will pass or block the message. Thought it was called bigtimer or timer or… will let you know as soon as I can acces my node red interface.

Edit: time range node you should use.

I don’t see how a time range-node will help with what I’m trying to accomplish.
What I’m trying to do is: If motion is ON, based on current time, the light turns on (brightness 30% or 100%).

I’ve also tried using a change-node to change payload to time. That also doesn’t work.

https://flows.nodered.org/node/node-red-contrib-time-range-switch

Something like this?
Between dusk and dawn the light goes up to 5%, outside that it will be at 100%.
After 10 minutes it will turn off again.

[{"id":"f3647d2.0f8cb8","type":"mqtt in","z":"edd650e8.42545","name":"Motion Hallway Upstairs","topic":"zigbee2mqtt/MotionHallwayUpstairs","qos":"2","datatype":"auto","broker":"f90cfe3b.a8bd7","x":100,"y":340,"wires":[["cdc220d5.52b07"]]},{"id":"9a25eb6d.7f3d28","type":"function","z":"edd650e8.42545","name":"Turn on 5%","func":"if ( msg.payload.includes(\"\\\"occupancy\\\":true\"))\n{\n    msg.payload = {\n        domain: \"light\",\n        service: \"turn_on\",\n        data: {\n            entity_id: \"light.light_hallway_upstairs\",\n            \"brightness_pct\": 5\n        }\n    };\n}\nelse\n{\n    msg.payload = \"do nothing\";\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":240,"wires":[["a051b267.101f8","e9c5c82d.3121d8"]]},{"id":"cc10b57f.624518","type":"mqtt in","z":"edd650e8.42545","name":"Motion Hallway MasterBedroom","topic":"zigbee2mqtt/MotionMasterBedroom","qos":"2","datatype":"auto","broker":"f90cfe3b.a8bd7","x":130,"y":180,"wires":[["cdc220d5.52b07"]]},{"id":"5f197612.f8ecd8","type":"delay","z":"edd650e8.42545","name":"","pauseType":"delayv","timeout":"10","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":760,"y":240,"wires":[["e5f70fb1.21368"]]},{"id":"a051b267.101f8","type":"function","z":"edd650e8.42545","name":"Reset 10m Delay","func":"msg.delay = 600000;\nvar m1 = {reset:true};\nreturn [[m1,msg]];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":590,"y":240,"wires":[["5f197612.f8ecd8"]]},{"id":"e9c5c82d.3121d8","type":"switch","z":"edd650e8.42545","name":"Only turn on","property":"payload.service","propertyType":"msg","rules":[{"t":"cont","v":"turn_on","vt":"str"}],"checkall":"true","repair":true,"outputs":1,"x":570,"y":280,"wires":[["ba4fd5e1.b2b4e8"]]},{"id":"4e97820e.b921dc","type":"server-state-changed","z":"edd650e8.42545","name":"","server":"37d68302.2aabcc","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.light_hallway_upstairs","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":440,"y":160,"wires":[["a051b267.101f8"],[]]},{"id":"cdc220d5.52b07","type":"time-range-switch","z":"edd650e8.42545","name":"","lat":"51.83125","lon":"4.33552","startTime":"dusk","endTime":"dawn","startOffset":0,"endOffset":0,"x":110,"y":260,"wires":[["9a25eb6d.7f3d28"],["873b5209.56274"]]},{"id":"873b5209.56274","type":"function","z":"edd650e8.42545","name":"Turn on 100%","func":"if ( msg.payload.includes(\"\\\"occupancy\\\":true\"))\n{\n    msg.payload = {\n        domain: \"light\",\n        service: \"turn_on\",\n        data: {\n            entity_id: \"light.light_hallway_upstairs\",\n            \"brightness_pct\": 100\n        }\n    };\n}\nelse\n{\n    msg.payload = \"do nothing\";\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":280,"wires":[["e9c5c82d.3121d8","a051b267.101f8"]]},{"id":"ba4fd5e1.b2b4e8","type":"api-call-service","z":"edd650e8.42545","name":"payload light","server":"37d68302.2aabcc","version":1,"debugenabled":false,"service_domain":"homeassistant","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":890,"y":280,"wires":[[]]},{"id":"e5f70fb1.21368","type":"function","z":"edd650e8.42545","name":"Turn off","func":"msg.payload = {\n    domain: \"light\",\n    service: \"turn_off\",\n    data: {\n        entity_id: \"light.light_hallway_upstairs\"\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":920,"y":240,"wires":[["ba4fd5e1.b2b4e8"]]},{"id":"f90cfe3b.a8bd7","type":"mqtt-broker","z":"","name":"","broker":"192.168.0.9","port":"1883","clientid":"node-red","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"37d68302.2aabcc","type":"server","z":"","name":"Home Assistant"}]

A time range node has 2 outputs. One within the time, one outside.

Yup, that is also how i solved it :wink:

@sender, @Kermit, @aceindy; thank you very much. It works like a charm! I had difficulties to think of an inside- and outside range. I was merely focussed on specifying time ranges.

This was ment to be my follow-up question, but I found it out already: For anyone wanting to use more than 2 or 3 variables: You can tie multiple time range-nodes together. Source