Hi there,
On my script.yaml I have a series of script like this:
script.light_1
script.light_2
and so on…
each script is like this:
light_1:
alias: Simulator - light 1 - small lamp living room
sequence:
- service: switch.turn_on
entity_id: switch.small_lamp
- delay: "00:{{ '{:02}'.format(range(1,4) | random | int) }}:00"
- service: switch.turn_off
entity_id: switch.small_lamp
light_2:
alias: Simulator - light 2 - led stripe bed room
sequence:
- service: light.turn_on
entity_id: light.led_bed_room
- delay: "00:{{ '{:02}'.format(range(1,4) | random | int) }}:00"
- service: light.turn_off
entity_id: light.led_bed_room
- service: light.turn_off
entity_id: light.led_bed_room
and so on…
then I created an automation that SHOULD turn on the light on my house when my security system is armed and sun.sun is “below the horizon”. All timings have to be randomly fired, precisely:
- the chosen light (light_1… light_2 etc.) - one by one
- the period of time the light is turned on
- the period of time between each light
So, I did this automation:
- alias: Simulator - light looper
trigger:
- platform: state
entity_id: alarm_control_panel.kjr_partition_0
to: 'armed'
for:
seconds: 120
- platform: state
entity_id: sun.sun
from: 'above_horizon'
to: 'below_horizon'
condition:
condition: and
conditions:
- condition: state
entity_id: input_boolean.presence_simulator #I used this for other stuff....
state: 'off'
- condition: state
entity_id: alarm_control_panel.kjr_partition_0
state: 'armed'
- condition: state
entity_id: sun.sun
state: 'below_horizon'
action:
repeat:
while:
- condition: and
conditions:
- condition: state
entity_id: alarm_control_panel.risco_giuseppe_rizzo_partition_0
state: 'armed_home'
- condition: state
entity_id: alarm_control_panel.kjr_partition_0
state: 'armed'
- condition: state
entity_id: sun.sun
state: 'below_horizon'
sequence:
- service: script.turn_on
data_template:
entity_id: "script.light_{% set rand = {{ range(1,10) | random}} %}"
- delay: "00:{{ '{:02}'.format(range(2,21) | random | int) }}:00"
I DOES work but - of course - the automation turn the other light without turning off the previous ones…
I thought I need some kind of “wait_template” so I tried something like this:
sequence:
- service: script.turn_on
data_template:
{% set rand = {{ range(2,5) | random}} %}
entity_id: "script.light_{% set rand = {{rand}} %}"
wait_template: "{{ is_state('script.light_{{rand}}', 'off') }}"
- delay: "00:{{ '{:02}'.format(range(1,3) | random | int) }}:00"
I even tried this:
sequence:
- service: script.turn_on
data_template:
{% set rand = {{ range(2,5) | random}} %}
entity_id: "script.light_{% set rand = {{rand}} %}"
- wait_template: "{{ is_state('script.light_{{rand}}', 'off') }}"
- delay: "00:{{ '{:02}'.format(range(1,3) | random | int) }}:00"
but still no luck. I got:
while scanning for the next token found character ‘%’ that cannot start any token in “/config/automations.yaml”, line 1406, column 14
So… I’m here… please, anyone can give a good advice?
Thanks a lot for your attention
Gekomax