Create automation depending condition call one script or other

Hi,

In my corridor i have 2 wifi bulbs. At night, i have an automation that calls a script to turn on only 1 bulb for a few minutes and then turn off.

I want that this automation, makes balancing of using the bulbs, because i want that both have the same times (more or less) that turn on and turn off.

It’s possible to make just one automation for all?

It’s possible in one automation call 1 script or other depending on condition?

thanks

Yes, but I’d have one script that I’d conditionally pass the bulb’s entity_id to.

What do you want to base the decision on? E.g., does the script run every hour, and the decision can be based on whether the current hour is odd or even? Or do you need something like an input_boolean to keep track of which bulb to turn on?

Hi,

I’m thinking to use an input_boolean to select which bulb use.

imagen

Now i have an automation that at 23:00 enable the use of the script and at 08:00 disable.

imagen

So i want just 1 automation that have condition if this input_boolean is on, call script_1 and if off, call script_2.

thanks

Pictures of the frontend are not all that helpful. Could you please post your existing automations and scripts related to this topic? And when you do, please follow the instructions at the top of the page so they format properly.

Just put the entities into a random selection template in the automation. See the templating docs.

Hi,

This is my automation now, where always call 1 script (turn on and off the same bulb alwys).

alias: Luz_Pasillo_Noche_Mov_Pasillo
hide_entity: False
initial_state: 'off'
trigger:
  platform: state
  entity_id: binary_sensor.motion_sensor_158d0001e057fa
  to: 'on'
action:
  service: homeassistant.turn_on
  entity_id: script.timed_lamp

And this is the script that i have running:

timed_lamp:
  alias: 'Enciende la lampara del Pasillo'
  sequence:
    - service: script.turn_off
      data:
         entity_id: script.timer_off
    - service: light.turn_on
      data:
        entity_id: light.yeelight_white_f0b429a8faf5
        brightness: 64

    # Set new timer
    - service: script.turn_on
      data:
        entity_id: script.timer_off

timer_off:
  alias: 'Apaga la lampara del pasillo despues de 3 minutos'
  sequence:
    - delay:
        minutes: 3
    - service: light.turn_off
      data:
        entity_id: light.yeelight_white_f0b429a8faf5

Now i want incorporate in the automation a condition with “Pasillo 1” frontend to call script of bulb 1 or bulb 2.
thanks

Hi,

Thanks for reply i will see templating docs about random selection.

You mean this: https://www.home-assistant.io/components/sensor.template/, because i didn’t see anything else about templating docs

thanks

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.

1 Like

A lot of thanks!! @pnbruckner

This weekend i will try this automation.

Hi,

I was reading and preparing the automation you did it, and i have a doubt:
In the condition automation you wrote:

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

It’s correct? Or maybe the second service must be script.turn_on and add entity_id: script.timed_lamp

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.turn_on**
    **entity_id: script.timed_lamp**
    data:
      lights:
        - light.yeelight_white_f0b429a8faf5
        - light.LIGHT2

thanks

See passing variables to scripts. I’m using “the other way” which is simpler.

Hi,

last week i prepared the automation, and at the beginning didn’t work. But 2 days ago i checked and now is working perfect! thanks a lot @pnbruckner!!

One thing more, are you using alexa? i want add haaska plugin into my HA docker…

No, sorry, I don’t use alexa or docker. You should probably start a new topic if you have questions about those.