Could somebody help me out with the following and why I get an error trying to create a random off time
- alias: Turn Off TV Simulator at random time
initial_state: true
trigger:
platform: time
at: "00:{{ range(0,5) | random | int }}{{ range(0,9) | random | int }}:00"
action:
service: switch.turn_off
entity_id:
- switch.sonoff_socket_6
id: turntvsimulatoroffontimer
Well this will never work.
You are asking it to generate a random time AND fire at that time that it just generated (every minute)
So the chances of it actually being that time when the clock changes every minute is 1 in 60 for each minute.
But you are not actually passing a datetime value
What you need to do is generate a number at (say) 00:00 (midnight) or better still at 23:59 and then store that in an input_datetime location then feed that location to your trigger and Robert Is your Avuncular Relative
Read up on input_datetime and trigger templates
Mutt
You definitely cannot use a template in the time trigger’s at parameter.
I’d do something like the following. (In fact, I have many automations that do this sort of thing.)
- alias: Turn Off TV Simulator at random time
initial_state: true
trigger:
platform: time
at: '00:00:00'
action:
- delay: "{{ (range(0, 60+1) | random) * 60 }}"
- service: switch.turn_off
entity_id:
- switch.sonoff_socket_6
id: turntvsimulatoroffontimer
Note that “range(x, y) | random” returns a random value between x and y-1. So:
delay: "{{ (range(0, 60+1) | random) * 60 }}"
Gets a random number from 0 to 60 (i.e., minutes), multiplies it by 60 (to get seconds), and then delays that number of seconds. I.e., it delays a random number of minutes from 0 to 60. Or to get a random number of seconds from 0 to the number of seconds in 60 minutes: