Can't figure out how to use an ELSE statement to simplify automations

Hi team,
I’m just starting to wrap my head around more complicated automations, and I’m trying to build one that essentially only turns the light on when a. I am home, AND b. it’s between the hours of 06:00 and 23:00.

Based on one test, I think I’ve got something that works. But pretty sure I could simplify this if I could just wrap my head around how to use ELSE statements, i.e., IF user is home AND hours between 06:00 and 23:00 turn light on ELSE turn light off.

I have read pages and pages of documentation, and have managed to break (and restore) my Home Assistant a couple times already and just need someone to highlight what I’m missing.

Thanks in advance. Here’s the script I’ve got so far:

trigger:
  - platform: state
    entity_id:
      - person.user1
    to: home
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - platform: state
    entity_id:
      - person.user1
    to: not_home
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - platform: time
    at: "23:00:00"
  - platform: time
    at: "06:00:00"
condition: []
action:
  - if:
      - condition: state
        entity_id: person.user1
        state: not_home
      - condition: or
        conditions:
          - condition: time
            after: "06:00:00"
            before: "23:00:00"
    then:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.bad_light
  - if:
      - condition: state
        entity_id: person.user1
        state: home
      - condition: time
        after: "06:00:00"
        before: "23:00:00"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.bad_light
  - if:
      - condition: time
        after: "23:00:00"
        before: "06:00:00"
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.bad_light
mode: single

I believe this should work:

trigger:
  - platform: state
    entity_id:
      - person.user1
    to: home
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: home
  - platform: state
    entity_id:
      - person.user1
    to: not_home
    for:
      hours: 0
      minutes: 5
      seconds: 0
    id: not_home
  - platform: time
    at: "23:00:00"
    id: time_off
  - platform: time
    at: "06:00:00"
    id: time_on
condition: []
action:
  - if:
      - condition: state
        entity_id: person.user1
        state: home
      - condition: time
        after: "06:00:00"
        before: "23:00:00"
      - condition: or
          - conditions:
            - condition: trigger
              id: time_on
            - condition: trigger
              id: home
	then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.bad_light
    else:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.bad_light
mode: single
1 Like

Being home and the time is between 06:00 and 23:00 seem like conditions to me rather than a trigger?

What do you want to trigger the light coming on?

You arriving home or the time hitting 06:00 when you’re already home? Some other activity?

are you replying to me or the OP?

This worked and cleared up my confusion at the same time. Thank you very much!

Now to rebuild my many other automations with this new technique :slight_smile:

1 Like