Help with automation simulation

Hi
I would like to simulate presence every night between 00:30 - to 06:00 in the night
but each time to turn on 1 light
after xx minutes I would like to turn off the light that previous turned on and turn on the 2nd light of my group (I have 5 lights in my group)

I found a piece of code to start with and I change it a little
Can someone confirm that it will work or an idea how to implement this

#############################################################################
## Turn on/off indoor lights randomly 
#############################################################################
- id: Night_Lights
  alias: Night_Lights
  initial_state: True
  hide_entity: False

  trigger:
    - platform: time_pattern
      minutes: '/10'
    

    - condition: time
      after: '00:30:00'
      before: '05:50:00'
    

  action:
    - delay: "00:{{ '{:02}'.format(range(0, 30) | random | int) }}:00"
    #- delay: "00:00:{{ '{:02}'.format(range(0, 10) | random | int) }}"
    - service: homeassistant.toggle
      data_template:
        entity_id: "{{ state_attr('group.night_simulation','entity_id') | random }}"

################################################################
## Turn off indoor lights randomly between 
################################################################
- id: Turn off all lights
  alias: Turn off all lights
  initial_state: True
  hide_entity: False

  trigger:
    - platform: time
      minutes: '/15'
      
  - condition: time
      after: '00:40:00'
      before: '06:00:00'

  action:
    - delay: '00:{{ range(10,18) | random | int }}:00'
    - service: homeassistant.turn_off
      entity_id: group.night_simulation

Create an input_number in configuration.yaml

input_number:
  light_sequence:
    initial: 0

Now use this input_number to keep track of which light we are on in the sequence.

What you have is going to do weird things by having this trigger faster than the delay. Every 10 minutes, it’s going to skip the previous trigger’s delay effectively only doing this in 10 minute patterns. Thus, your max delay should be less than your trigger time.

There’s also no need for a second automation to turn off the lights. Just make the same one turn it on/off.

#############################################################################
## Turn on/off indoor lights randomly 
#############################################################################
- id: Night_Lights
  alias: Night_Lights
  initial_state: True
  hide_entity: False

  trigger:
    - platform: time_pattern
      minutes: '/15'
    
    - condition: time
      after: '00:30:00'
      before: '05:50:00'
    
  action:
    # Every 15 to 29 minutes.
    - delay: "00:{{ '{:02}'.format(range(0, 14) | random | int) }}:00"
    - service: homeassistant.turn_off
      data_template:
        entity_id: "{{ state_attr('group.night_simulation')[states('input_number.light_sequence') | int] }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.light_sequence
        # Add 1 and wrap around. 
        value: "{{(states('input_number.light_sequence') + 1 ) % {{ expand('group.night_simulation') | list | count }}"
   # Wait a few seconds
   - delay: "00:00:{{ '{:02}'.format(range(1, 5) | random | int) }}"
   # Turn on the next thing in the sequence
   - service: homeassistant.turn_on
     data_template:
       entity_id: "{{ state_attr('group.night_simulation')[states('input_number.light_sequence') | int] }}"

Thanks. I will try it asap. Meanwhile if it is not trouble can you explain a bit the input number concept because I can not understand it.

for example can you give me please the values that the input number will take in the above automation?

i pasted the code in automation.yaml and i get

bad indentation of a sequence entry at line 323, column 6:
         delay: "00:00:{{ '{:02}'.format( ... 

The input number will be used to store the index of the light that was last turned on.

It will be a number from 0 to the size of the number of lights in the group - 1. This will allow us to control the order in which the lights turn on/off. The original one you posted only picked a random light.

Also, yeah…I see my additional -delay and -service are off by one space.

  action:
    # Every 15 to 29 minutes.
    - delay: "00:{{ '{:02}'.format(range(0, 14) | random | int) }}:00"
    - service: homeassistant.turn_off
      data_template:
        entity_id: "{{ state_attr('group.night_simulation')[states('input_number.light_sequence') | int] }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.light_sequence
        # Add 1 and wrap around. 
        value: "{{(states('input_number.light_sequence') + 1 ) % {{ expand('group.night_simulation') | list | count }}"
    # Wait a few seconds
    - delay: "00:00:{{ '{:02}'.format(range(1, 5) | random | int) }}"
    # Turn on the next thing in the sequence
    - service: homeassistant.turn_on
      data_template:
        entity_id: "{{ state_attr('group.night_simulation')[states('input_number.light_sequence') | int] }}"

ok. I restarted HA and got the following

Fri Feb 21 2020 17:20:27 GMT+0200 (Eastern European Standard Time)
Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['action'][2]['data_template']['value']. Got None
required key not provided @ data['trigger'][1]['platform']. Got None. (See /config/configuration.yaml, line 11). 
Invalid config for [input_number]: required key not provided @ data['input_number']['light_sequence']['max']. Got None
required key not provided @ data['input_number']['light_sequence']['min']. Got None. (See /config/configuration.yaml, line 90).

Whoops, min and max are required for the input_number.

input_number:
  light_sequence:
    initial: 0
    min: 0
    # Just make something large so adding additional lights to the 
    # group we wont have to come update this value.
    max: 100

Hope we are close
now I got this

Fri Feb 21 2020 17:46:32 GMT+0200 (Eastern European Standard Time)
Invalid config for [automation]: required key not provided @ data['trigger'][1]['platform']. Got None. (See /config/configuration.yaml, line 11).

if I paste in template

"{(states('input_number.light_sequence') + 1 ) % {{ expand('group.night_simulation') | list | count }}"

i get = Unknown error rendering template ( i don’t know how to fix it)

in the second

"{{(states('input_number.light_sequence') + 1 ) % {{ expand('group.night_simulation') | list | count }}"

again error – but without the first { – I get %6

"{(states('input_number.light_sequence') + 1 ) % {{ expand('group.night_simulation') | list | count }}"

the 3rd one again gives error

"{{ state_attr('group.night_simulation')[states('input_number.light_sequence') | int] }}"

Sorry, didn’t convert to int. And had wrong syntax for expand…

value: "{{(states('input_number.light_sequence') | int + 1 ) % {{ expand(states.group.night_simulation) | list | count }}"

sorry but something is wrong here too

“{{ state_attr(‘group.night_simulation’)[states(‘input_number.light_sequence’) | int] }}”

Hahaha…man I’m surprised I can even spell my name half the time.

"{{ state_attr('group.night_simulation', 'entity_id')[states('input_number.light_sequence') | int] }}"

I hope to stay a little more with me

Fri Feb 21 2020 18:47:08 GMT+0200 (Eastern European Standard Time)
Invalid config for [automation]: required key not provided @ data['trigger'][1]['platform']. Got None. (See /config/configuration.yaml, line 11). 

I changed the code to this

## Turn on/off indoor lights randomly 
- id: Night_Lights
  alias: Night_Lights
  initial_state: True
  trigger:
    platform: time_pattern
    minutes: '/15'
  condition:
    condition: time
    after: '19:00:00'
    before: '05:50:00'
  action:
    # Every 15 to 29 minutes.
    - delay: "00:{{ '{:02}'.format(range(0, 14) | random | int) }}:00"
    - service: homeassistant.turn_off
      data_template:
        entity_id: "{{ state_attr('group.night_simulation', 'entity_id')[states('input_number.light_sequence') | int] }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.light_sequence
        # Add 1 and wrap around. 
        value: "{{(states('input_number.light_sequence') | int + 1 )}} % {{ expand(states.group.night_simulation) | list | count }}"
    # Wait a few seconds
    - delay: "00:00:{{ '{:02}'.format(range(1, 5) | random | int) }}"
    # Turn on the next thing in the sequence
    - service: homeassistant.turn_on
      data_template:
        entity_id: "{{ state_attr('group.night_simulation', 'entity_id')[states('input_number.light_sequence') | int] }}"

I restarted HA no problem in the logs but when I trigger the automation won’t work. ( I changed the to after 19:00 in order to see if it is working)

please note that I there was a problem with

"{{(states('input_number.light_sequence') | int + 1 ) % {{ expand(states.group.night_simulation) | list | count }}"

and added 2 more }} like the following
“{{(states(‘input_number.light_sequence’) | int + 1 )}} % {{ expand(states.group.night_simulation) | list | count }}”

now the 3 templates I THINK that are reporting correct but the automation won’t work

"switch.k1_kitchen"
"1 % 7"
"switch.k1_kitchen"

thank you!
Final working code.

## Turn on/off indoor lights randomly 
- id: Night_Lights
  alias: Night_Lights
  initial_state: True
  trigger:
    platform: time_pattern
    minutes: '/12'
  condition:
    condition: time
    after: '19:00:00'
    before: '05:50:00'
  action:
    # Every 12 to 20 minutes.
    - delay: "00:{{ '{:02}'.format(range(0, 8) | random | int) }}:00"
    - service: homeassistant.turn_off
      data_template:
        entity_id: "{{ state_attr('group.night_simulation', 'entity_id')[states('input_number.light_sequence') | int] }}"
    - service: input_number.set_value
      data_template:
        entity_id: input_number.light_sequence
        # Add 1 and wrap around. 
        value: "{{(states('input_number.light_sequence') | int + 1 ) %  expand(states.group.night_simulation) | list | count }}"
    # Wait a few seconds
    - delay: "00:00:{{ '{:02}'.format(range(1, 5) | random | int) }}"
    # Turn on the next thing in the sequence
    - service: homeassistant.turn_on
      data_template:
        entity_id: "{{ state_attr('group.night_simulation', 'entity_id')[states('input_number.light_sequence') | int] }}"

Hi, the above code is working fine. I was wondering if you could help me to make it a little better, but I am not sure how to do it.

  1. Ideally I would like to choose which light to open randomly. Right now the switches are openinng one by one in the same order. eg 1-2-3-4 in the next round it goes with the same pattern. I think it would be better to follow a randomly or at least another order eg 2-1-4-3

  2. At 05:50:00 (the end of the automation) the last opened light remains open. Here I know that I can add one more automation to switch them off. I wondering though if it could be in the same automation

However If it is a lot of trouble for you I will keep it this way. I think the 1st request is not that simple

Thanks