I am new to HA and just trying to automate my lights to turn on or off at a random time between two time or after a cirtain hour. So for example, i want my light in my living room window to turn on around 16:00. But with a random time between 15:45 and 16:15 for example. Is this possible and how would i go about doing this? I am a complete noob when it comes to HA and it was a loong time ago since i last touched any coding.
and if it is possible to combine and autumation for a light to turn on at a random time as described and then turn off at a random time in the same event or what to say that would be awesome aswell!
Assuming you want to do this to have lights turn on and off while you’re away, there is a very good Presence Simulation HACS integration that might help you.
You can also use this app with the app daemon addon, it just replays your activity from the past to make it look like you’re home. You get your normal behavior pattern this way.
Yes i want the lights to turn on before I get home after work and before it gets dark outside here in sweden. And then turn off during the night after I go to bed so around 02-03 or so. I will definitely test out the trigger and Chek out the two apps aswell.
alias: "Light "
description: sätter på/ stänger av Light random tid
trigger:
- id: "on"
platform: time
at: "15:45:00"
- id: "off"
platform: time
at: "03:00:00"
action:
- delay:
minutes: "{{ range(0,31)|random }}"
- service: light.turn_{{ trigger_id }}
target:
entity_id: light.buren
data: {}
It does not seem to work, i can see in the logg and traces that it triggerd at 03:00. But the light was still on when i woke up this morning. have i missed something in the trigger or is it something else?
New randomized time values will be generated at the start of every day and whenever Template entities are reloaded.
Use them in your automation like this:
alias: "Light "
description: sätter på/ stänger av Light random tid
trigger:
- id: "on"
platform: time
at: sensor.buren_on
- id: "off"
platform: time
at: sensor.buren_off
action:
- service: 'light.turn_{{ trigger.id }}'
target:
entity_id: light.buren
The advantage of this method is that it is less vulnerable to a restart compared to a delay. If Home Assistant is restarted while the delay is counting down, the delay will be terminated and the automation will not execute the next action (in other words it wouldn’t turn the light on or off).
NOTE
There’s a mistake in your existing automation.
- service: light.turn_{{ trigger_id }}
^
|
This should be a period not an underscore