Random Time Trigger?


#1

I’m looking for an easy way in Node-RED to trigger a light turning on at a random time during a certain window, say 7-8 pm. I was thinking that I could set a flow variable with random number once a way which would be the minutes and then have a poll node that runs every minute which checks for the time and sees if the hour is 19:00 and th minute is the random minute. Is there an easier way to do this like a node that already exists for this?

Thanks!


#2

I don’t know if there is a node for this but what you can do, use the cron node to trigger at 7pm then function node to generate a random number between 0–3600 and set it in msg.delay. Add delay node after then the service call.


#3

Here’s my really simple vacation light flow.

[{"id":"5599676c.5155f8","type":"inject","z":"1a9590f5.2d236f","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"*/6 16-23 * * *","once":false,"onceDelay":0.1,"x":102,"y":896,"wires":[["3ba4dac9.6346b6"]]},{"id":"28da8444.3b90dc","type":"time-range-switch","z":"1a9590f5.2d236f","name":"","lat":"","lon":"","startTime":"sunset","endTime":"23:59","startOffset":0,"endOffset":0,"x":432,"y":896,"wires":[["5f7b539e.52b0bc"],[]]},{"id":"b978892c.263648","type":"ha-get-entities","z":"1a9590f5.2d236f","server":"2dad33ee.42bf5c","name":"","rules":[{"property":"entity_id","logic":"in_group","value":"group.vacation_lights","valueType":"str"},{"property":"state","logic":"is","value":"off","valueType":"str"}],"output_type":"random","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":742,"y":896,"wires":[["b0feaaae.490768"]]},{"id":"5f7b539e.52b0bc","type":"function","z":"1a9590f5.2d236f","name":"25%","func":"const random = Math.round(Math.random() * 100);\n\nif(random < 75) {\n    node.status({fill: \"red\", text: random});\n    return null;\n}\nnode.status({fill: \"green\", text: random});\nreturn msg;","outputs":1,"noerr":0,"x":594,"y":880,"wires":[["b978892c.263648"]]},{"id":"b0feaaae.490768","type":"api-call-service","z":"1a9590f5.2d236f","name":"Turn On","server":"2dad33ee.42bf5c","service_domain":"homeassistant","service":"turn_on","data":"{\"entity_id\":\"{{payload.entity_id}}\"}","render_data":true,"mergecontext":"","output_location":"","output_location_type":"none","x":892,"y":896,"wires":[["7febf921.5d03f8"]]},{"id":"7febf921.5d03f8","type":"delay","z":"1a9590f5.2d236f","name":"","pauseType":"random","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"5","randomLast":"36","randomUnits":"minutes","drop":false,"x":1052,"y":880,"wires":[["200079df.0a9b06"]]},{"id":"200079df.0a9b06","type":"api-call-service","z":"1a9590f5.2d236f","name":"Turn Off","server":"2dad33ee.42bf5c","service_domain":"homeassistant","service":"turn_off","data":"{\"entity_id\":\"{{payload.entity_id}}\"}","render_data":true,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":1196,"y":896,"wires":[[]]},{"id":"3ba4dac9.6346b6","type":"api-current-state","z":"1a9590f5.2d236f","name":"vaca mode","server":"2dad33ee.42bf5c","outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is_not","override_topic":true,"entity_id":"input_boolean.vacation_mode","state_type":"str","override_payload":true,"override_data":true,"x":262,"y":896,"wires":[["28da8444.3b90dc"],[]]}]

#4

A few questions, if you don’t mind. The 25% is just a way so that it only fires 25% of the time? And you have your vacation lights turn on and off randomly? So it’s possible that the same light turns on and off repeatedly, right?

I’m not a Node/JavaScript programmer otherwise I’d whip up a node that does exactly what I want. :grinning:


#5

Thanks! That seems simple enough! It actually looks like the delay node can do a random delay which makes it even easier.


#6

Yes so it only fires 25% of the time it is triggered. Just remove the get-entities node and and hard code the entities in the call-service node that you want to turn on and off


#7

Thanks! I’ll give it a try. It also looks like Big Timer may work using a random offset.


#8

Maybe you want to try this new node :slight_smile: … I like the options you can choose to control your random behavior!


#9

That node looks pretty sweet!


#10

I think that pretty much nailed what I want to do! Thanks!

[Updated: This is absolutely a killer node! Thanks!]