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
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
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)
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
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).
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
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
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