Your implementation seems overly complicated. You could do everything in one automation and one script. Using @ironlion27’s suggest of randomly selecting the bulb, here is a suggested implementation.
automation:
alias: Luz_Pasillo_Noche_Mov_Pasillo
hide_entity: False
trigger:
platform: state
entity_id: binary_sensor.motion_sensor_158d0001e057fa
to: 'on'
condition:
condition: time
after: '23:00:00'
before: '08:00:00'
action:
# Stop script if it's still running.
- service: script.turn_off
entity_id: script.timed_lamp
# Start script, passing in list of lights to use.
- service: script.timed_lamp
data:
lights:
- light.yeelight_white_f0b429a8faf5
- light.LIGHT2
script:
timed_lamp:
sequence:
# Turn off all lights in case one was left on if this script
# was stopped while it was in the delay step.
- service: light.turn_off
data_template:
entity_id: "{{ lights|join(',') }}"
# Turn on a random light from the list of lights to use.
- service: light.turn_on
data_template:
entity_id: "{{ lights|random }}"
brightness: 64
# Leave light on for a while.
- delay: '00:03:00'
# Turn light off (by turning all the listed lights off.)
- service: light.turn_off
data_template:
entity_id: "{{ lights|join(',') }}"
The automation uses a condition to allow it to run only during the period between 23:00 and 08:00. (I.e., you don’t need other automations to turn it on and off.) It first makes sure the script is stopped in case it was still running (e.g., the motion sensor trips twice within three minutes.) Then it starts the script, passing in a list of the lights from which to choose. You provided the entity_id for one of the lights, but not the other. So change light.LIGHT2
accordingly.
The script first turns off all of the listed lights (in case it was stopped after it had turned on one of them on, but before it got a chance to turn it back off.) Then it turns on one of the lights chosen at random. Then it waits three minutes. Then it turns the light back off. It does this by turning all the listed lights off. This way it doesn’t have to “remember” which one was turned on.
The only “tricky” part is the templates in the script. The data passed in the variable named lights
is a list of entity_id’s, where each entity_id is a string. Unfortunately you can’t use that directly (e.g., entity_id: "{{ lights }}"
) because that would make the entity_id option a string like "['light.LIGHT1', 'light.LIGHT2']"
, which would not work. Fortunately the light.turn_on service does allow the entity_id option to be a string which contains light entity_id’s separated by commas. That’s what "{{ lights|join(',') }}"
does. It creates a string like this: 'light.LIGHT1,light.LIGHT2'
. Notice the string does not contain the square bracket characters at the beginning and end of the string, or the “inside” quote characters. This is an acceptable form of input.
The other “tricky” template is "{{ lights|random }}"
. This simply selects one of the strings in the lights
list.