Home Assistant Community

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!

0 Likes

#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.

0 Likes

#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"],[]]}]
0 Likes

#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:

0 Likes

#5

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

0 Likes

#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

0 Likes

#7

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

0 Likes

#8

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

1 Like

#9

That node looks pretty sweet!

0 Likes

#10

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

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

0 Likes

#11

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.

0 Likes

#12

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

0 Likes

#13

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

Thanks,

0 Likes

#14

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

0 Likes