Same trigger but different actions depending on condition

Hello,

I’m having a motion sensor and two lamps which should be turned on (either or) depending on the horizon-state of the sun component.
What I currently have and what works is the following:

- alias: "Bathroom / Motion Sensor (Cabinet)"
  trigger:
    platform: state
    entity_id: binary_sensor.bathroom__motion_sensor
    to: "on"
  action:
    - service: light.turn_on
      entity_id: light.bathroom__light__cabinet
  condition:
    condition: state
    entity_id: sun.sun
    state: 'below_horizon'

- alias: "Bathroom / Motion Sensor (Ceiling)"
  trigger:
    platform: state
    entity_id: binary_sensor.bathroom__motion_sensor
    to: "on"
  action:
    - service: light.turn_on
      entity_id: light.bathroom__light__ceiling
  condition:
    condition: state
    entity_id: sun.sun
    state: 'above_horizon'

However that basically creates 2 motion sensors in HA as well as lot’s of redundant code.

What I’d rather like to have - and what would conform much more to reality (there is just one motion sensor) - would be sth. like this:

- alias: "Bathroom / Motion Sensor"
  trigger:
    platform: state
    entity_id: binary_sensor.bathroom__motion_sensor
    to: "on"
  action:
    - condition:
      condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - service: light.turn_on
      entity_id: light.bathroom__light__cabinet
    - condition:
      condition: state
      entity_id: sun.sun
      state: 'above_horizon'
    - service: light.turn_on
      entity_id: light.bathroom__light__ceiling

However that fails as I apparently can’t have multiple conditions of the same kind (state).

How to to consolidate both conditional actions into one automation?

Thanks in advance!

mirko

1 Like

I would suggest using templating to decide which entity_id to turn on:

  action:
    - service: light.turn_on
      data_template:
        entity_id: >
          {% if is_state('sun.sun', 'below_horizon') %}
            light.bathroom__light__cabinet
          {% else %}
            light.bathroom__light__ceiling
          {% endif %}
3 Likes

I have three mostly redundant automations to turn my kitchen lights on to different brightness based on sunset and time: 1) 6am to sunset = 100% brightness, 2) sunset to 9pm = 50% brightness, and 3) 9pm to 11pm = 20% brightness.

Now that I have an understanding of writing automations in yaml, applying templates like this would be a great next educational step. I’ve been reading docs and examples but haven’t quite figured it out.

  1. data_template: should work for brightness, too, right? Or maybe I should be using a value_template?

  2. Do you know how to use now()? I know I’m doing that wrong.

  3. Is there a way to do something like before: sunset in one of these templates? If not, is there a better solution than what I’m trying to do below, which is test for 6am to 4pm since sunset doesn’t occur before 4pm and then 4pm to sunset and setting 100% brightness for both? (sometimes the sun is below horizon after 6am, but I still want 100% brightness in the morning)

    action:
    service: light.turn_on
    entity_id: light.kitchen
    data_template:
    brightness: >
    {% if now(> ‘06:00:00’) and now(< ‘16:00:00’) %}
    255
    {% elif now(> ‘16:00:00’) and (‘sun.sun’, ‘above_horizon’) %}
    255
    {% elif is_state(‘sun.sun’, ‘below_horizon’) and now(> ‘16:00:00’) and now(< ‘21:00:00’) %}
    127
    {% else %}
    50
    {% endif %}

Thanks a lot, that serves exactly the purpose!

I don’t know if you ever figured out how to use now() ?
If not, then here is an example!

data_template:
  brightness: >
  {% if ('sun.sun', 'above_horizon') and now().hour >= 10 and now().hour < 18 %}
  255
  {% else %}
  56
  {% endif %} 

This will set the brightness of a given light to 255 between 10:00 and 18:00 and else it will be 56.

Cheers!