I don’t personally use delays so I’m not certian if the numbers need to have leading zeros when under 10. If that works, then that is what @Henrik1986 should use.
Sorry. This is the config that gave me the errors. And yes home assistant will have this format HH:MM:SS or HH:MM
alias: TEST
trigger:
platform: state
entity_id: input_boolean.trigger
to: 'on'
action:
- delay '00:{{ '{:02}'.format(range(1,10) | random | int) }}:00'
- service: homeassistant.toggle
data_template:
entity_id: '{{ states.group.away_lights.attributes.entity_id | random }}'
Home assistant will have two numbers in minutes also.
Your issue is because you have quotes inside quotes. You cannot encapsolate single quotes inside single quotes. You need to use double quotes for the entire template and single quotes inside the template. Also, you are missing the colon after the word delay. Remember, yaml is space and punctuation driven, without that, yaml doesn’t know how to parse properly.
alias: TEST
trigger:
platform: state
entity_id: input_boolean.trigger
to: 'on'
action:
- delay: "00:{{ '{:02}'.format(range(1,10) | random | int) }}:00"
- service: homeassistant.toggle
data_template:
entity_id: '{{ states.group.away_lights.attributes.entity_id | random }}'
Thanks for the lesson. Learned a lot today. Thank you
Here is the code cleaned up… note conditions are by default AND so that part wasn’t needed.
################################################################
## Turn on/off indoor lights randomly while night and armed away
################################################################
- alias: Random Away Lights
initial_state: True
hide_entity: False
trigger:
- platform: time
minutes: '/20'
seconds: 00
condition:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: 'armed_away'
- condition: sun
after: sunset
action:
- delay: '00:{{ range(1,19) | random | int }}:00'
- service: homeassistant.toggle
data_template:
entity_id: "{{ state_attr('group.indoor_random_away_lights','entity_id') | random }}"
It did, but there is change that it won’t work because home assistant will have the time delay in this two formats.
HH:MM:SS or HH:MM
So if the random delay is 8 min home assistant will not fire the action.
So I made some changes after some days. It’s pretty annoying that lights is turn off or on when i come home because the random delay. So i did a script with the conditions that it’s only execute when the alarmpanel is disarmed after that a random lamp is turned off or on.
But one question. Is it possible to send brightness level in the action section in the scripts. when the lamp is turned of and then turned on is comes back on 100 %.
alias: Belysning - inte hemma
trigger:
platform: time
minutes: '/45'
seconds: 00
condition:
condition: and
conditions:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: 'armed_away'
- condition: sun
after: sunset
action:
- delay: "00:{{ '{:02}'.format(range(1,30) | random | int) }}:00"
- service: script.turn_on
entity_id: script.away
Script
away:
alias: Away lights
sequence:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: 'armed_away'
- service: homeassistant.toggle
data_template:
entity_id: '{{ states.group.away_lights.attributes.entity_id | random }}'
You’d have to turn the group into a light group if you want to pass brightness to the lights in the group. Also, they’ll all have to be lights.
Thanks for asking this question as it’s given me inspiration to set this up myself!
For those interested here is my complete “random light” code.
It also includes automations for sunrise and alarm disarm to turn off the lights it’s randomly turned on.
As always suggestions and improvements welcome! I learn so much from this community – it truly is fantastic.
################################################################
## Random lights on while sunset and armed away
################################################################
- alias: Random Away Lights
initial_state: True
hide_entity: False
trigger:
- platform: time
minutes: '/30'
seconds: 00
condition:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: 'armed_away'
- condition: sun
after: sunset
action:
- delay: "00:{{ '{:02}'.format(range(1, 29) | random | int) }}:00"
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: 'armed_away'
- service: homeassistant.toggle
data_template:
entity_id: "{{ state_attr('group.indoor_random_away_lights','entity_id') | random }}"
##########################################################
## Sunrise Random Lights Off
##########################################################
- alias: Random Lights Off Sunrise
initial_state: True
hide_entity: False
trigger:
- platform: sun
event: sunrise
condition:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: 'armed_away'
action:
- service: homeassistant.turn_off
entity_id: group.indoor_random_away_lights
##########################################################
## Alarm Disarmed Random lights Off
##########################################################
- alias: Random Lights Off Disarmed
initial_state: True
hide_entity: False
trigger:
- platform: state
entity_id: alarm_control_panel.ha_alarm
from: 'armed_away'
to: 'disarmed'
action:
- service: homeassistant.turn_off
entity_id: group.indoor_random_away_lights
Does this work? I thought you needed to ensure that hh, mm and ss were always two digits.
I do this:
- delay: "00:{{ '{:02}'.format(range(5, 11) | random | int) }}:00"
Apparently that’s up for debate because many people claim they are using it without the leading zero.
Maybe their random number has never been one digit long
murphy’s law right there!
My guess is that when HA see’s a time, it parses the time based on colons. If it parsed that way, it would work fine.
It seems to work and is used a couple times a day in various automations; and they are all working.
But I do agree yours is the proper solution to ensure it’s 2 digits for each field and I’ll happily update.
Thanks for sharing. Haven’t you got the state of the alarm panel under conditions in there twice unnecessarily?
condition:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: 'armed_away'
- condition: sun
after: sunset
action:
- delay: "00:{{ '{:02}'.format(range(1, 29) | random | int) }}:00"
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: 'armed_away'
- service: homeassistant.toggle
The second one is to ensure the light is turned on after the delay only if the alarm is still armed. During that delay if you disarm the light would still come on without that 2nd one.
Thanks a heap. Didn’t realise (after 2+ yrs of using HA), that you can have a condition after an action! Awesome. P.S If you can edit your code above, the time statement is no longer valid. It now needs to be:
trigger:
- platform: time_pattern
minutes: '/30'