Random light automation (presence simulator)

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

      sequence:
        - service: script.turn_on
          data:
            entity_id: script.light_{{ range(2,5) | random }}
        - delay: "00:{{ '{:02}'.format(range(1,3) | random | int) }}:00"

data_template has been deprecated. Use data. Your variable instantiation needs to be in the same template it is being used. Why did you try to set rand twice? You really don’t need to use a variable at all. wait_template is not valid in a service call. What is the delay for? What happens after the delay?

I can’t test this I’m at work but I think it’s ok. You should be able to use this example to fix the rest.

Hi, thanks, I’ll try. I’ post the results.

the “wait” is because I want that the first light turns off before the automation turns on the next one. Without the “wait” the automation repeat the service call (aka turns on a new light) before the script is ended so there are more than one light on at the same time.

The delay after the service call is because I want a variable period of time of darkness in between

Sorry i did a mistake, the automation action is like this, the is not double set:

      sequence:
        - service: script.turn_on
          data_template:
            {% set rand = {{ range(2,5) | random}} %}
            entity_id: "script.light_{{rand}}"
            wait_template: "{{ is_state('script.light_{{rand}}', 'off') }}"
        - delay: "00:{{ '{:02}'.format(range(1,3) | random | int) }}:00"

Thanks
Gekomax

Just saw your post and wondering, if you have right now a final radnom automation which works according your needs. I am still looking for some similiar automation for a random light. thank you - JJ