Presence automation with multiple conditions

Hi All,

I am trying to create a simple automation, I want the entrance light to turn on only if two people are at home, I know there are other ways to do that but I want to use “and”, later I would like to create a similar automation but with “or” in order to trigger the action only if one of the people is home.

In the following code snippet I am testing the automation with only one person at home and it triggers the light although I am using “and”, Can someone tell me what is wrong with my configuration?

automation:
  alias: Arriving Home
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: device_tracker.shai
    state: 'home'
  condition:
    condition: and
    conditions:
      condition: state
      entity_id: device_tracker.alina
      state: 'home'
  action:
    service: switch.turn_on
    entity_id: switch.entrance_light

Thank you in advance.

In this case you use one person as a trigger, and one as a condition. Therefor, you don’t need to use a and in your condition since you only have one condition.

You could do as follow:

automation:
  alias: Arriving Home
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: device_tracker.shai
      state: 'home'
    - platform: state
      entity_id: device_tracker.alina
      state: 'home'
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: device_tracker.shai
      state: 'home'
    - condition: state
      entity_id: device_tracker.alina
      state: 'home'
  action:
    service: switch.turn_on
    entity_id: switch.entrance_light

This should normally turn on your light when both the device are detected and either of the device come back home.

But the easy way would be:

automation:
  alias: Arriving Home
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: device_tracker.shai
      state: 'home'
    - platform: state
      entity_id: device_tracker.alina
      state: 'home'
  action:
    service: switch.turn_on
    entity_id: switch.entrance_light

So that it simply turn on the light when either one of you come back home

If you have sun light where this lamp is placed, you could add a condition to avoid it being turned on during the day:

    condition:
      condition: and
      conditions:
        - condition: sun
          after: sunset
          after_offset: "-1:00:00"
        - condition: time
          after: '15:00:00'
          before: '23:00:00'

Prior to using this, make sure your “sun” component is active, in configuration.yaml:

# Track the sun
sun:

Thank you so much, I have tried the first example and I get an “Invalid config” message

2017-06-02 16:10:42 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: required key not provided @ data[‘action’]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 519). Please check the docs at Automation - Home Assistant

The reason I use the first example is because I want to do the same only with one of two people are in the house

Yes, I’ve edited my message just after publishing it, check it again, the action part was missing.

What do you want? That it only turn it on when nobody else’s home?
For that you should use a or condition with state “not_home”.
If you use a “or” for state home, it will always be triggered since at least one person will be home (or it won’t be triggered in the first place).

Or that it always turn on when someone come home? (not matter if someone is already home)

Thanks your example is working now,

How can I test this automation properly?

I clicked “trigger” when “shai” is at home and alina is away and the light turned on, it looks like the “and” was “ignored”, any idea why?

I am referring to several possible situations:

  1. turn on the light only if Shai and Alina are home
  2. turn on the light only if Shai is home
  3. turn on the light only if Alina is home

Thanks!

That’s 3 opposite scenario. You want the automation to make those 3 together? Than take the simple automation without condition.

When you click on trigger on an automation the condition are ignored. It’s only a way to test the action, not the condition :slight_smile:

If you want to test the condition and the trigger, you should simply disconnect your phone from the wifi and wait for the state to change in HA. Reconnect it and see if it turns the light on or not.

The fact that the manual trigger ignore condition also bugged me at first, it almost render this manual trigger useless (at least for testing).

upvote for this

Thank you so much for your help, I tested everything and it is working great i will use the “or” to check if one of us arrives home and then turn on the light, the next step is will be to add another condition so it will be on between 6PM to 9PM only :slight_smile:

I tried something like this:

automation 9:
  alias: Arriving Home
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: device_tracker.shai
      state: 'home'
    - platform: state
      entity_id: device_tracker.alina
      state: 'home'
  condition:
    condition: or
    conditions:
    - condition: state
      entity_id: device_tracker.shai
      state: 'home'
    - condition: state
      entity_id: device_tracker.alina
      state: 'home'
    - condition: time
          after: '17:00:00'
          before: '20:00:00'  
          weekday:
            - sun
            - mon
            - tue
            - wed
            - thu   
  action:
    service: switch.turn_on
    entity_id: switch.kitchen_light

and something like this:

automation 9:
  alias: Arriving Home
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: device_tracker.shai
      state: 'home'
    - platform: state
      entity_id: device_tracker.alina
      state: 'home'
  condition:
    condition: or
    conditions:
    - condition: state
      entity_id: device_tracker.shai
      state: 'home'
    - condition: state
      entity_id: device_tracker.alina
      state: 'home'
  condition:
    condition: and
    conditions:
    - condition: time
          after: '17:00:00'
          before: '20:00:00'  
          weekday:
            - sun
            - mon
            - tue
            - wed
            - thu   
  action:
    service: switch.turn_on
    entity_id: switch.kitchen_light

Both ignores the “time” condition, I am confused with the way conditions works apparently…any idea what i am doing wrong here ?

Thanks!

EDITED: (there were yaml errors in the code. I removed them, the following is fine now)

first of all, if the trigger is the presence of one of the two devices, the conditions “if one of the two devices is home” is basically useless (as it is already you trigger)
So you can simply get rid of the OR condition

So you can just put

automation 9:
  alias: Arriving Home
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: device_tracker.shai
      state: 'home'
    - platform: state
      entity_id: device_tracker.alina
      state: 'home'
  condition:
    condition: time
      after: '17:00:00'
      before: '20:00:00'  
      weekday:
        - sun
        - mon
        - tue
        - wed
        - thu   
  action:
    service: switch.turn_on
    entity_id: switch.kitchen_light

this will trigger the automation when any of you gets home, if its before 20:00 on the defined days…

On your first example, it ignores the time condition, because basically you say “when some one gets home, then check if someone gets home OR if its the right time”. As the condition “then check if someone gets home” is automatically fulfilled when someone gets home, it does not need to check the time. (I hope it is clear :p)

Basically the rule is:

OR condition (at least 1 is true, then condition is true)
AND condition (all are true, then condition is true)

Thanks, It’s driving me crazy, I tried your suggestion and I get the following error:

Jun 02 17:31:56 raspberrypi systemd[1]: Started Home Assistant for homeassistant.
Jun 02 17:32:03 raspberrypi hass[2540]: 2017-06-02 17:32:03 ERROR (Thread-1) [homeassistant.util.yaml] while parsing a block mapping
Jun 02 17:32:03 raspberrypi hass[2540]: in "/home/homeassistant/.homeassistant/configuration.yaml", line 698, column 3
Jun 02 17:32:03 raspberrypi hass[2540]: expected <block end>, but found '<block mapping start>'
Jun 02 17:32:03 raspberrypi hass[2540]: in "/home/homeassistant/.homeassistant/configuration.yaml", line 707, column 4
Jun 02 17:32:03 raspberrypi hass[2540]: 2017-06-02 17:32:03 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
Jun 02 17:32:03 raspberrypi hass[2540]: in "/home/homeassistant/.homeassistant/configuration.yaml", line 698, column 3
Jun 02 17:32:03 raspberrypi hass[2540]: expected <block end>, but found '<block mapping start>'
Jun 02 17:32:03 raspberrypi hass[2540]: in "/home/homeassistant/.homeassistant/configuration.yaml", line 707, column 4
Jun 02 17:32:03 raspberrypi hass[2540]: Config directory: /home/homeassistant/.homeassistant

Yeah I put a bit too much spaces at some points in the code (YAML is very strict on the number of spaces.) I edited it, try again :wink:

Anyway, keep in mind that you should always try to find the simplest automation as possible. Basically you are trying here to turn on the lights when anyone gets home. So there is no need to be fancy with AND/OR conditions or anything else. The simple two-trigger solution is already enough.
By the way, instead of a time condition, you should check the sun condition (in summer it is not dark enough at 20:00, while in winter it is already night in 17:00). Personnaly, I use a numeric_state and check the sun elevation to decide if it is “dark enough” to light on the rooms.

Arghhhh…The neverending yaml errors, i use sublime it looks like indentation is fine but now i get this:

Jun 02 17:38:45 raspberrypi systemd[1]: Started Home Assistant for homeassistant.
Jun 02 17:38:52 raspberrypi hass[2766]: 2017-06-02 17:38:52 ERROR (Thread-1) [homeassistant.util.yaml] mapping values are not allowed here
Jun 02 17:38:52 raspberrypi hass[2766]: in "/home/homeassistant/.homeassistant/configuration.yaml", line 708, column 10
Jun 02 17:38:52 raspberrypi hass[2766]: 2017-06-02 17:38:52 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: mapping values are not allowed here
Jun 02 17:38:52 raspberrypi hass[2766]: in "/home/homeassistant/.homeassistant/configuration.yaml", line 708, column 10
Jun 02 17:38:52 raspberrypi hass[2766]: Config directory: /home/homeassistant/.homeassistant

Well, the error is in line 708 in your configuration file. What is in this line?

after: ‘17:00:00’

oops my bad again…
It should be

condition:
  condition: time
    after: '17:00:00'
    before: '20:00:00'  
....

Still no luck :frowning: errors again i’m starting to feel really dumb :slight_smile:

Jun 02 17:53:42 raspberrypi systemd[1]: Started Home Assistant for homeassistant.
Jun 02 17:53:49 raspberrypi hass[2948]: 2017-06-02 17:53:49 ERROR (Thread-1) [homeassistant.util.yaml] mapping values are not allowed here
Jun 02 17:53:49 raspberrypi hass[2948]: in "/home/homeassistant/.homeassistant/configuration.yaml", line 709, column 12
Jun 02 17:53:49 raspberrypi hass[2948]: 2017-06-02 17:53:49 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: mapping values are not allowed here
Jun 02 17:53:49 raspberrypi hass[2948]: in "/home/homeassistant/.homeassistant/configuration.yaml", line 709, column 12
Jun 02 17:53:49 raspberrypi hass[2948]: Config directory: /home/homeassistant/.homeassistant


697: automation 9:
698:   alias: Arriving Home
699:   initial_state: 'on'
700:   trigger:
701:     - platform: state
702:       entity_id: device_tracker.shai
703:       state: 'home'
704:     - platform: state
705:       entity_id: device_tracker.alina
706:       state: 'home'
707:   condition:
708:     condition: time
709:       after: '17:00:00'
710:       before: '20:00:00'  
711:       weekday:
712:         - sun
713:         - mon
714:         - tue
715:         - wed
716:         - thu   
717:   action:
718:     service: switch.turn_on
719:     entity_id: switch.kitchen_light

Ok It was indeed line 709

707:   condition:
708:     condition: time
709:       after: '17:00:00'
710:       before: '20:00:00'  
711:       weekday:
712:         - sun
713:         - mon
714:         - tue
715:         - wed
716:         - thu   

It should be:

707:   condition:
708:     condition: time
709:     after: '17:00:00'
710:     before: '20:00:00'  
711:     weekday:
...

@lambtho Thanks for helping I really appreciate it!