Random Time Trigger?

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!

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.

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"],[]]}]
1 Like

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:

1 Like

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

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

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

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

1 Like

That node looks pretty sweet!

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

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

Have you had any issues with the presence-faker node? I started using it but have a few of the nodes randomly start to turn on even though my vacation mode is off. The vacation mode is what inputs the TRUE the presence-faker nodes need to start. The other presence-faker nodes that control other lights don’t seem to be effected.

I’ve even tried to delete the nodes that randomly turn on certain lights and rebuild it but have the same issue.

Can you share your flow? … I have not experienced any random turn-on activity

I might have fixed it by updating the Node-Red Docker container. I’ll test it tonight to see what happens.

Thanks,

The upgrade hasn’t worked. I tried getting the .json for this particular flow but the flows.json combines all the flows together and I’m not experienced enough to look for this one flow. How can I get this one flow?

edit: I re-built the flow and it seems to be working as expected. Thanks

@xx_Nexus_xx thanks for the tip using this node! I’ve set up a vacation mode using presence faker which is “almost” perfect… I’m trying to work out how to adjust the start time based on if it is sunset/dark which of course changes throughout the year. Any thoughts?

I have not tested yet but you can change start and end time dynamically. Just use a node to get the time for sunset/dawn of the day and update the fake-presence node accordingly

Thanks @xx_Nexus_xx. I should have looked deeper into this - I was not aware you could inject dynamic content! I’m not a coder but will have a play and see if I can get it to work.

@sooty were you ever able to inject a dynamic start time using sunset?

Hi @gullygossner sadly I was distracted and didn’t get around to it. However, I recently trashed my Home Assistant instance upgrading HASS to 3.10 (SD corruption) so I am now in the process of rebuilding all my nodered automations so will take another look at it. If I manage to get it working I’ll gladly share my code with you.

Just in case somebody is looking for a way to do it with the integrated nodes… just use the delay node and select: random delay.
Then specify the from - to timeframe. i.e: 2 - 5 minutes. Whenever a message gets in, this will be delayed randomly by minimum 2 and maximum 5 minutes. I use this to randomize my snooze button for my alarmclock^^