Good morning,
Working on an automation where a plug activates after a certain timeframe and when one or the other device is at home. The GUI does not really allow me to do that as the WHEN statement is only when a device ENTERS a home, not is AT home.
I’ve tried to adapt the YAML code to reflect the same but it does not seem to work.
Can anyone guide me in the right direction ?
All triggers in HA are Event based. Just “being” at home is not an event… coming home is an event, coming home and staying there for X time is an event, etc.
Because of the way you have broken up the automation, it hard to tell what you have actually done. Instead of copy/pasting each portion, click on the three-dot menu button on the top right of the Automation editor, select “Edit in YAML”, copy everything and share that so we can see how the whole is constructed.
Is it the timeframe that starts the process, with the requirement that one or both devices are home, or is it the process of arrival of either what’s important?
And to confirm: must only one of the devices be home at a time, or can it be both too?
When (the trigger) is something that happens. The and if stuff are conditions — things that are or needs to be true when the when occurs.
The general pattern for what I think you want to do is to trigger on either device arriving home, plus a condition for either to be home.
Indeed … the idea is that the monitor turns off when both devices are out of the house. Once one of the devices comes back in, the monitor turns back on. Once I have this completely buttoned up, I’ll have a look to see how I can make this action only valid between sunrise and 22h00 as then the house goes to “sleep”.
For that you will want to assign both as triggers.
alias: Dakboard on when 1 pax home
description: ""
triggers:
- device_id: aefbc981826c9eccd732e0ef458718db
domain: device_tracker
entity_id: 0d6d71f5a39c754c779a179b32186282
type: enters
trigger: device
zone: zone.home
- device_id: 9bb13ed2d61f8ee8a258d30761b63be0
domain: device_tracker
entity_id: f9ff7adb689ef0e3eb50cdf6c6a91c5d
type: enters
trigger: device
zone: zone.home
conditions:
- alias: Check that it's between sunrise and 22h00
condition: time
after: sensor.sun_next_rising
before: "22:00:00"
- alias: Check that the switch is "off"
condition: state
entity_id: 824bc780de9a5b711334e9f1ff702744
state: "off"
actions:
- type: turn_on
device_id: 02c455f9f9313bd6f8f30a49e6f0d423
entity_id: 824bc780de9a5b711334e9f1ff702744
domain: switch
mode: single
One thing you may want to do is to assign the device trackers to person entities.
The Person and Zone integrations work together so that the state of any zone is a count of “persons” in that zone. This can make presence-based automations much easier because the automation only needs to watch or check one thing. For example, the two triggers above would be reduced to:
Using “persons” can also reduce code maintenance, long-term. You can add or subtract device trackers from a person (like when you get a new phone) without having to alter every related automation, script, or dashboard card individually.