Hi,
I’m about to write some first automations that go beyond turn on bla at 6 am and need some guidance for best practice.
Let’s say I want to have my espresso maker be on between 6 am and 14:00 whenever anyone is home.
I can see two scenarios for the trigger:
trigger: time is 6 am
condition: someone home
and
trigger: someone comes to an empty home
condition: it is between 6 and 14
Do I need two separate automations here or can one combine them into one through some OR statement? But I don’t think there is an OR statement that could combine trigger and conditions.
Then how do I deal with switching the coffee machine off? Do I need two more automations with essentially the same triggers
trigger: last person leaves
condition: []
and
trigger: it is 14:00
condition: []
at least this one is clearly doable with one OR statement since there are no conditions.
One efficiency I have managed to think of is that the on trigger launches a script which waits for the first of 14:00 or everyone leaving. Is this the right use case?
I guess it would be easier if you could do set a state (scene?) of “coffee machine on” whenever both presence and time are satisfied and define the actions for entering this state and leaving it (switch coffee machine on/off). Then it is just one set of conditions.
Or am I just overcomplicating and this is just what the trigger is: some OR of (someone home) and (time between 6 and 14) and even though this is true for half a day rather than on a state change, it will just work?
You can combine both triggers and both conditions in one automation.
Alternatively you can configure a template sensor that checks for these conditions and returns a true or false. You can then use that to trigger this automation or multiple automations.
Personally it’s easier to adjust the triggers in an automation so I would probably use that. But instead of having the times in there. I’d have a daytime input Boolean. I’d then use a scheduler card to set the times for that.
So the automation would have two triggers.
Presence goes to home
Daytime Boolean goes on
Two conditions
Presence is home
Daytime Boolean is on
An action to turn it off.
You’d then need a way to turn it off, so you could use another similar automation or it is also possible to combine both into one, by determining the action based on the to.state
But how do I do it? Where do I put my AND/ORs? Or is it actually simple:
trigger:
- someone get home
- it is 6 am
condition:
conditions: and
- someone at home
- it is between 6 and 14
action: turn it on!
which is what you added to your reply 
No need for the and conditions are and by default.
trigger:
- someone get home
- it is 6 am
condition:
- someone at home
- it is between 6 and 14
action: turn it on!
You can think of multiple triggers being "OR"d, and by default multiple conditions are "AND"d. So, like @samnewman86 said, you can simply list them.
One other thing to consider: when HA (re)starts, if the triggers are “satisfied”, the automation won’t actually fire because a trigger typically has to change from “unsatisfied” to “satisfied” to fire. Hence you might also want to have a trigger that fires when HA starts. So something like:
- trigger:
- platform: homeassistant
event: start
- platform: state
entity_id: binary_sensor.SOMEONE_IS_HOME
to: 'on'
- platform: time
at: '06:00:00'
condition:
- condition: state
entity_id: binary_sensor.SOMEONE_IS_HOME
state: 'on'
- condition: time
after: '06:00:00'
before: '14:00:00'
action:
- service: switch.turn_on
entity_id: switch.ESPRESSO
If you want to combine the on and off, you could do it something like this:
- trigger:
- platform: homeassistant
event: start
- platform: state
entity_id: binary_sensor.SOMEONE_IS_HOME
- platform: time
at:
- '06:00:00'
- '14:00:00'
action:
- service: >
{% if is_state('binary_sensor.SOMEONE_IS_HOME', 'on')
and 6 <= now().hour < 14 %}
switch.turn_on
{% else %}
switch.turn_off
{% endif %}
entity_id: switch.ESPRESSO
These, of course, assume you create a binary sensor that indicates when at least one person is home. You could also do this with a group.
The first automation is pretty straightforward. The second is a bit more advanced; let me know if you have any questions.
1 Like
That’s brilliant; templating I’ve managed to figure out roughly already, so it makes sense.
Basically: either I have a separate script for each of on/off with a simple action, or I collect all the triggers for both off->on and on->off, and then have a branch in the action which essentially is the condition block and runs the on-action if still satisfied and off otherwise.
Let me start off by shrinking the multiple automations i’ve written for each trigger separately. I knew there had to be a more efficient way!
OK one more question:
If I have multiple (state) triggers is it possible to check in the action which one fired?
I’m trying to set some zwave params in my set of 4 identical devices, but I need to know which associated sensor changed the temperature and write it to just the one device, rather than writing to all four and wasting battery.
Yes you certainly can:
I would use a choose action:
Then option 1:
Trigger entity 1 -> action 1
Option 2,3,4 etc.
What is a choose action? Or do you mean just a bunch of if/then else branches?
You can also choose a choose in the actions dropdown