Turning off lights based on state and platform template with multiple conditions

I was using an automation based on the below, but the state of not_home wouldn’t work as I have a zone created for work. As a result, if I was at work my state would be Work instead of not_home.

I’m trying to get this working based on the state platform for person.sara and the template for myself (person.greg)

However, I’m getting the error for the below, line 192 is the - condition: template line.

What am I missing?

Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/automations.yaml", line 192, column 9
expected <block end>, but found '<scalar>'
  in "/config/automations.yaml", line 193, column 43
#Turn off the inside lights when no one has been home for 20min between 8a-5pm Mon-Fri
- alias: 'Lights off No One Home'
  trigger: 
    - platform: state
      entity_id: 
        #- person.greg
        - person.sara
    - platform: template
        
  condition:
    condition: and
    conditions:
      - condition: time 
        after: '08:00:00'
        before: '17:00:00'
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
         
      - condition: template
        value_template: '{{ not is_state('person.greg','home') }}'
        #'{{states('person.greg')!='home'}}'
        #entity_id: person.greg
        #state: 'not_home'
      - condition: state
        entity_id: person.sara
        state: 'not_home'
        
        for: 
          minutes: 20
        
  action:
    - service: homeassistant.turn_off #Turn off the Inside Lights Group
      data:
        entity_id: group.inside_lights

There are quite a few errors in that automation. Rather than itemize them, could you explain what you’re ultimately trying to achieve? E.g., are you wanting to turn the group of lights off 20 minutes after both of you have left?

EDIT: Ah, the comment at the top of the automation seems to answer my question.

Probably the easiest way to do this is to create a group containing the two person entities. Let’s call it group.people. Then you could do this:

#Turn off the inside lights when no one has been home for 20min between 8a-5pm Mon-Fri
- alias: 'Lights off No One Home'
  trigger: 
    - platform: state
      entity_id: group.people
      to: 'not_home'
      for:
        minutes: 20
  condition:
    - condition: time 
      after: '08:00:00'
      before: '17:00:00'
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: homeassistant.turn_off #Turn off the Inside Lights Group
      data:
        entity_id: group.inside_lights

The group’s state should only be 'home' and 'not_home'.

1 Like

Thanks that’s helpful and it’s what I though would’ve had to be done by creating a group.

FWIW I was able to get a similar automation running by using the following without a group. I had to remove template from the trigger area.

- alias: "Set Thermostats to Away"
  initial_state: true
  trigger: 
    - platform: state
      entity_id: 
        - person.greg
        - person.sara

  condition:
    condition: and
    conditions:
      - condition: time 
        after: '08:00:00'
        before: '17:00:00'
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
      #- condition: state
      #  entity_id: person.greg
      #  state: 'not_home'
      - condition: template
        value_template: "{{ not is_state('person.greg','home') }}"
      
      - condition: state
        entity_id: person.sara
        state: 'not_home'

  action:
    - service: climate.set_preset_mode
      data:
        entity_id: climate.upstairs
        preset_mode: 'Away'
 
    - service: climate.set_preset_mode
      data:
        entity_id: climate.downstairs
        preset_mode: 'Away' 

Here’s another way without a group:

- alias: "Set Thermostats to Away"
  initial_state: true
  trigger: 
    - platform: template
      value_template: >
        {{ not is_state('person.greg','home') and
           not is_state('person.sara','home') }}
      for:
        minutes: 20
  condition:
    - condition: time 
      after: '08:00:00'
      before: '17:00:00'
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  action:
    - service: climate.set_preset_mode
      data:
        entity_id:
          - climate.upstairs
          - climate.downstairs
        preset_mode: 'Away'
1 Like

Brilliant. I had it backwards on what went into trigger vs what went in to conditions. I didn’t gather that clarity from the docs.
Going to mark this one as the solution as it’s closer to original without creating a group.
Thanks!