Help understanding automation - "at home" for multiple people

Hi,

I lifted the basics of the following ‘automations.yaml’ entries from https://home-assistant.io/docs/automation/trigger/ - but there are several aspects I just don’t understand, and this wording frankly confused me greatly:

STATE TRIGGER
Triggers when the state of tracked entities change. If only entity_id given will match all state changes, even if only state attributes change.

It would greatly help me (and hopefully others too) if someone could kindly answer my questions regarding the following entries:

- id: A_HomeMode_Home
  alias: "Camera Home-Mode automation - arriving home"
  initial_state: 'on'
  trigger:
    platform: state
    entity_id:
      - device_tracker.kelly
      - device_tracker.matt
    from: 'not_home'
    to: 'home'
    for:
      hours: 0
      minutes: 10
      seconds: 0
  action:
    service: switch.turn_on
    data:
      entity_id: switch.synology_home_mode

- id: A_HomeMode_Away
  alias: "Camera Home-Mode automation - leaving home"
  initial_state: 'on'
  trigger:
    platform: state
    entity_id:
      - device_tracker.kelly
      - device_tracker.matt
    from: 'home'
    to: 'not_home'
    for:
      hours: 0
      minutes: 10
      seconds: 0
  action:
    service: switch.turn_off
    data:
      entity_id: switch.synology_home_mode
  1. These show up as switches on the main Overview page. Does toggling these switches on and off trigger the actions? Or does it enable/disable the automation?
  2. There are several people involved, and for this idea to work the “Home mode” switch would be turned on if either of us were at home, but only turned off if both of us were away. How is that distinction handled?

Many thanks!

Matt

Hi Matt,

changing the status of the switches will turn the automation on and off - something you normally don’t want to do or at least not have done by somebody else by accident. That’s why I normally don’t show them in my UI.

The home mode is best managed through a group because that actually does exactly what you want it to do: it shows ‘the group’ as being home as soon as one member of the group is home and shows it as being away only if all members of the group are away. So, put the devices of Matt and Kelly into a group and then use the group as the trigger.

Depending on what you use as the tracker for your devices it might already take 10min or so before the status changes - at least that’s the case for my Unifi Controller. It changes a device to ‘not_home’ after ~10min but changes it to ‘home’ within 30sec or so.

Have fun!

Examples:

in my groups.yaml I use

people:
  name: People & Devices
  entities:
    - device_tracker.her_device 
    - device_tracker.my_device

in my automation.yaml I refer to them

- alias: Actions when people come home
  trigger:
    platform: state
    entity_id: group.people
    to: 'home'
  action:
   - service: homeassistant.turn_on
       entity_id: switch.mplug2
3 Likes

I use a group called, group.family, for my device trackers. When one leaves, and one is home, the group still shows as home, and when one arrives home it will also show the group at home. Then, use that in an automation in this way.

eg:
# Leaving Home - Turn Everything Off #
- alias: Leaving Home - Turn Everything Off
  trigger:
    platform: state
    entity_id: group.family
    from: 'home'
    to: 'not_home'
    for:
      minutes: 1
  action:
    - service: homeassistant.turn_off
      entity_id:
        - light.hall
        - light.steps
        - light.table
        - switch.fish_tank
        - switch.lamps
        - switch.tv
        - switch.bedroom_light
        - switch.sonoff_1
    - service: scene.turn_on
      entity_id: scene.kitchen_off
    - service: notify.jason
      data:
        message: "Everything has been turned off"
    - service: switch.turn_on
      entity_id: switch.vacuum_power
    - delay: '00:00:30'
    - service: vacuum.turn_on
      entity_id: vacuum.xiaomi_vacuum_cleaner
1 Like

Thanks mate that was the perfect steer!!

Great community here :slight_smile:

Thanks to Kanga as well, good info

1 Like

Hi - one more question please team - is there a way to manually set the group state to ‘not_home’ so I can run some troubleshooting? I have tried modifying the group object manually within ‘/config/customize’.

When we leave home, the group state changes fine (as evidenced in the logging) but for some reason the Automation is not kicking off. Is there something I have done wrong?

Thanks again and in advance :slight_smile:

EDIT - I checked the http://IP:Port/config/automation section, and it was empty. Turned out, to have the automations visible in the http://IP:Port/config/automation section, you need the “- id:” switch. It will all probably work now…

automations.yaml :

# Arriving Home
- id: arriving
  alias: Arriving Home
  trigger:
platform: state
entity_id: group.us
from: 'not_home'
to: 'home'
for:
  minutes: 3
  action:
- service: switch.turn_on
  entity_id:
    - switch.synology_home_mode
- service: switch.turn_off
  entity_id:
    - switch.foscam_motion
    - switch.ipwebcam_motion

# Leaving Home
- id: leaving
 alias: Leaving Home
  trigger: 
platform: state
entity_id: group.us
from: 'home'
to: 'not_home'
for:
  minutes: 3
  action:
- service: switch.turn_off
  entity_id:
    - switch.synology_home_mode
- service: switch.turn_on
  entity_id:
    - switch.foscam_motion
    - switch.ipwebcam_motion