Condition & trigger based automation

I have to working automations that I tried to combine into one and use trigger IDs but whatever combination comes first the default action - which is none - is executed.

What I want to achieve:

trigger 1: sun-->sunset
trigger 2: Android TV to 'on'
option 1: if it is sunset AND Android TV is switched to 'on' switch on the lights behind the couch
option 2: if Android TV is 'on' and sun sets switch on the lights behind the couch

My attempt to make the automation work:

alias: 'Wohnzimmer: Fernsehlicht'
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: 0
    id: sunset
  - platform: state
    entity_id:
      - select.thirteenhub_activities
    to: Android TV
    id: harmony
    attribute: options
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: sunset
          - condition: state
            entity_id: select.thirteenhub_activities
            state: Android TV
            attribute: options
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.couch
      - conditions:
          - condition: trigger
            id: harmony
          - condition: sun
            after: sunset
          - condition: state
            entity_id: select.thirteenhub_activities
            attribute: options
            state: Android TV
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.couch
    default: []
mode: single

Fixed it myself. The ‘options’ messed it up as they listed all possible states rather than confirming the choice.

Here’s what’s working right now:

alias: 'Wohnzimmer: Fernsehlicht'
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: 0
    id: sunset
  - platform: state
    entity_id:
      - select.thirteenhub_activities
    to: Android TV
    id: harmony
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: sunset
          - condition: state
            entity_id: select.thirteenhub_activities
            state: Android TV
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.couch
      - conditions:
          - condition: trigger
            id: harmony
          - condition: sun
            after: sunset
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.couch
    default: []
mode: single

If I have understood your automation’s logic correctly, it can be reduced to this:

alias: 'Wohnzimmer: Fernsehlicht'
description: ''
trigger:
  - platform: sun
    event: sunset
  - platform: state
    entity_id: select.thirteenhub_activities
    to: 'Android TV'
condition:
  - "{{ is_state('sun.sun', 'below_horizon') }}"
  - "{{ is_state('select.thirteenhub_activities', 'Android TV') }}"
action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.couch

To get my head around it:

If one trigger occures it is tested against both conditions and as one condition matches the state of the trigger that condition will be ‘ignored’ and only the other condition is being tested and if true the light turns on? And of course vice versa for the other trigger.

If my interpretation of your code is right you’re assumption is right and your code will become my code. :slight_smile:

The automation is triggered by either sunset or the select changing to ‘Android TV’.

After being triggered, the automation’s condition checks if it’s now after sunset and the select is set to ‘Android TV’. If both conditions are true, the light is turned on.

1 Like