Automation Nested Actions

So I have 2 Door sensors (Deck and Garage) that report Open and Closed and a Color Changing Smart Bulb (Light1) all setup already in Hassio and wanted to setup some Automatons to indicate the Door states

What I want to do is
Triggers: IF Deck or Garage Door set (change state) to OPEN
IF Deck AND Garage Door = Open Then >Light1 on Blue
ElseIF Deck = Open AND Garage = Closed Then >Light1 on Green
ElseIF Garage = Open AND Deck = Closed Then >Light1 on Red

Is there a way to do this with One automation or would I have to create three one for each of the Color States with the Trigger and Conditionals set up accordingly in them?

What attributes do you have available for the light?

Its a Color Changing bulb so I was using Call Service
light.turn_on with service data
color_name: red ect.

Excellent. This is how I would code it. Added an else statement to prevent an error. I am assuming the sensors are binary sensors with the opening class.

automation:
  - alias: 'light color'
    trigger:
      - platform: state
        entity_id: binary_sensor.deck
        to: 'on'
      - platform: state
        entity_id: binary_sensor.garage
        to: 'on'
    action:
      service: light.turn_on
      data_template:
        entity_id: light.light1
        color_name: >
          {% set d = states('binary_sensor.deck') %}
          {% set g = states('binary_sensor.garage') %}
          {% if d == 'on' and g == 'on' %}blue
          {% elif d == 'on' and g == 'off' %}green
          {% elif d == 'off' and g == 'on' %}red
          {% else %}yellow
          {% endif %}
1 Like

Awesome that worked perfectly! Thanks
So now thinking of going to next step that if one of the 2 sensors closes to re-evaluate sensor status to set the lights to the updated status

So if both doors open (light is Blue) and then say the Deck door closes leaving just garage open it would set the light back to Red.

I was thinking could it be done with a 2nd automation that was watching for either sensor to trip to OFF then trigger the same code but with an Option if both doors are OFF to turn the light off? But you would have to call a different service (light.turn_off) if both are OFF (closed)

It would change slightly. However, I’m not sure if sending a color_name after a turn_off would throw an error or turn the light back on. I think this would work.

Old example
automation:
  - alias: 'light color'
    trigger:
      - platform: state
        entity_id: binary_sensor.deck
      - platform: state
        entity_id: binary_sensor.garage
    action:
      service_template: >
        {% set d = is_state('binary_sensor.deck', 'off') %}
        {% set g = is_state('binary_sensor.garage', 'off') %}
        light.turn_o{% if d and g %}ff{% else %}n{% endif %}
      data_template:
        entity_id: light.light1
        color_name: >
          {% set d = states('binary_sensor.deck') %}
          {% set g = states('binary_sensor.garage') %}
          {% if d == 'on' and g == 'on' %}blue
          {% elif d == 'on' and g == 'off' %}green
          {% elif d == 'off' and g == 'on' %}red
          {% else %}black
          {% endif %}

EDIT 1: On second thought it would have to be split into a script for both actions.
EDIT 2: Might consider changing service call if deck or garage open, turn on else off. So that the default is turning off the light.

automation:
  - alias: 'light color'
    trigger:
      - platform: state
        entity_id: binary_sensor.deck
      - platform: state
        entity_id: binary_sensor.garage
    action:
      service_template: >
        {% set d = is_state('binary_sensor.deck', 'on') %}
        {% set g = is_state('binary_sensor.garage', 'on') %}
        script.light1_turn_o{% if d or g %}n{% else %}ff{% endif %}
      data_template:
        color: >
          {% set d = states('binary_sensor.deck') %}
          {% set g = states('binary_sensor.garage') %}
          {% if d == 'on' and g == 'on' %}blue
          {% elif d == 'on' and g == 'off' %}green
          {% elif d == 'off' and g == 'on' %}red
          {% else %}black
          {% endif %}

script:
  light1_turn_off:
    sequence:
      - service: light.turn_off
        entity_id: light.light1
  light1_turn_on:
    sequence:
      - service: light.turn_on
        entity_id: light.light1
        data_template:
          color_name: '{{ color }}'
2 Likes

Wow That Worked Perfect Thanks a ton!

1 Like