Device tracker, trigger on either one not_home, or both home, but not if one is home

How can you do this?

Basically I want the lights to turn on if either me or my wife comes home, or if we both come home at the same time, but do not trigger if one of us is already home.

My main problem is that when both of us come home at the same time (same car), I’ve found no way to check if the other one has already been home for some time, and both states seem to change instantaneously.

Below works if both of us don’t come home at the same time.


- alias: Turn on indoor lights when Me or Wife come home
  trigger:
    - platform: state
      entity_id: device_tracker.google_maps_me, device_tracker.google_maps_wife
      from: 'not_home'
      to: 'home'
  condition:
      condition: and
      conditions:
        - condition: or
          conditions:
            - condition: and
              conditions:
                      - condition: state
                        entity_id: device_tracker.google_maps_me
                        state: 'not_home'

                      - condition: state
                        entity_id: device_tracker.google_maps_wife
                        state: 'home'

            - condition: and
              conditions:
                      - condition: state
                        entity_id: device_tracker.google_maps_me
                        state: 'home'

                      - condition: state
                        entity_id: device_tracker.google_maps_wife
                        state: 'not_home'
        - condition: time
          before: '22:30:00'
          after: '06:30:00'
  action:
     - service: switch.turn_on
       entity_id: group.normal_lights
     - service: light.turn_on
       entity_id: group.normal_lights


Two ways.

  1. Use group.all_devices in the trigger. Assuming there are just the two device_tracker entities, this group will be home if either or both of you are home, and not_home if both of you are not home.
  2. Use a template trigger that "or"s your states.

For the second solution:

  trigger:
    platform: template
    value_template: >
      {{ is_state('device_tracker.google_maps_me', 'home') or
         is_state('device_tracker.google_maps_wife', 'home') }}

This will trigger whenever the first person comes home. Then it will not trigger again until you are both not home, and then someone comes home. Basically it will trigger whenever the result of the expression changes from false to true.

The approach I’d recommend is to create a group for the two trackers, and use the state of that group in your trigger.

Creating a group avoids problems that group.all_devices brings - it’s managed by Home Assistant, not you.

Thanks, worked like a treat

For posterity

automations.yaml


- alias: Turn on indoor lights when we both get home
  trigger:
    - platform: state
      entity_id: group.device_trackers
      from: 'not_home'
      to: 'home'
  condition:
      condition: and
      conditions:
        - condition: time
          before: '22:30:00'
          after: '06:30:00'
  action:
     - service: switch.turn_on
       entity_id: group.normal_lights
     - service: light.turn_on
       entity_id: group.normal_lights

- alias: Turn on indoor lights when Me or Wife get home
  trigger:
    - platform: state
      entity_id: device_tracker.google_maps_me, device_tracker.google_maps_wife
      from: 'not_home'
      to: 'home'
  condition:
      condition: and
      conditions:
        - condition: or
          conditions:
            - condition: and
              conditions:
                      - condition: state
                        entity_id: device_tracker.google_maps_me
                        state: 'not_home'

                      - condition: state
                        entity_id: device_tracker.google_maps_wife
                        state: 'home'

            - condition: and
              conditions:
                      - condition: state
                        entity_id: device_tracker.google_maps_me
                        state: 'home'

                      - condition: state
                        entity_id: device_tracker.google_maps_wife
                        state: 'not_home'
        - condition: time
          before: '22:30:00'
          after: '06:30:00'
  action:
     - service: switch.turn_on
       entity_id: group.normal_lights
     - service: light.turn_on
       entity_id: group.normal_lights


groups.yaml


device_trackers:
    name: Device Trackers
    view: false
    entities:
        - device_tracker.google_maps_me
        - device_tracker.google_maps_wife


That is way more complicated than it needs to be. This should work:

- alias: Turn on indoor lights when we get home
  trigger:
    - platform: state
      entity_id: group.device_trackers
      to: 'home'
  condition:
    - condition: time
      before: '22:30:00'
      after: '06:30:00'
  action:
    - service: homeassistant.turn_on
      entity_id: group.normal_lights

This will handle if either of you comes home while the other is out, or if you both come home at the same time (which, BTW, really can’t happen, since each change of state is processed separately.) You don’t need another automation.

And although I haven’t tried it myself, I’m pretty sure the homeassistant.turn_on service will handle a heterogeneous group of entities.

i’m trying to do the same thing and i only want to edit one file

would this work? it will be a few days before i have time to test it for real

alias: Tænd lys når hjemme
description: ''
trigger:
    platform: template
    value_template: >
      {{ is_state('person.tina_andersen', 'home') or
         is_state('person.bo_herrmannsen_5', 'home') }}
condition:
    condition: and
    conditions:
      - condition: or
        conditions:
          - condition: and
            conditions:
                    - condition: state
                      entity_id: person.tina_andersen
                      state: 'not_home'

                    - condition: state
                      entity_id: person.bo_herrmannsen_5
                      state: 'home'

          - condition: and
            conditions:
                    - condition: state
                      entity_id: person.tina_andersen
                      state: 'home'

                    - condition: state
                      entity_id: person.bo_herrmannsen_5
                      state: 'not_home'
      - condition: sun
        before: sunrise
        after: sunset
action:
  - type: turn_on
    device_id: ****
    entity_id: light.kitchen_1
    domain: light
    brightness_pct: 60
  - type: turn_on
    device_id: ****
    entity_id: light.kitchen_2
    domain: light
    brightness_pct: 60
mode: single

Why not just try what Phil and Tinkerer suggested?
What do you mean by only editing one file?

only want to edit automations.yaml

tried to create a group and failed

phil did suggest on Sep '19 2019:

of course not using groups creates a kind of double effect, but for a dummie like me i want to see things in a “simple” and direct way so to speak

might not be that for others than are “experts”, but hey we are all different “wired”

I just don’t know what to say…

It’s like seeing someone trying to walk through a wall right next to a door, and just keep pushing as if it’s the correct way to get in.

:man_shrugging:

If you add a group the group will take care of all logic and display it’s intention as the state.
Creating your own logic hides the intention and will make it hard to debug.

oki, will try adding a group again, but i got an error and could not solve it

Invalid config for [group]: [view] is an invalid option for [group]. Check: group->group->person_trackers->view. (See /config/configuration.yaml, line 8).

line 8 is empty
line 9 group: !include groups.yaml


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
  packages: !include_dir_named packages

It could be indentation error.

device_trackers seems to be missing one space? There should be two spaces then device_trackers:

And it seems you have eight spaces to the actual entities. There should probably be six

and i cant locate

Check: group->group->person_trackers->view

can i call it person trackers?

but so far i have:

  person_trackers:
    name: Person Trackers
    view: false
    entities:
       - person.tina_andersen
       - person.bo_herrmannsen_5

of course if it fails for that only reason i can rename things quickly
but tracking persons works for me… ie they change states like they should when i check it

A trick that should work to get a more meaningful error message is to combine the two yaml codes in to one. Example:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

#group: !include groups.yaml

group:
  device_trackers:
    name: Device Trackers
    view: false
    entities:
      - device_tracker.google_maps_me
      - device_tracker.google_maps_wife


automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
  packages: !include_dir_named packages

Now it should point at the correct line.
Once you got it correct you can simply copy it back to the groups file and revert the configuration.yaml back to its original state.

thanks for the tip

its the first line… ie group:

group:
  device_trackers:
    name: Device Trackers
    view: false
    entities:
      - device_tracker.google_maps_me
      - device_tracker.google_maps_wife

hmmm

You noticed that I commented the other “group” line?

yes and so did i


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

#group: !include groups.yaml

group:
  device_trackers:
    name: Device Trackers
    view: false
    entities:
      - device_tracker.google_maps_me
      - device_tracker.google_maps_wife

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
  packages: !include_dir_named packages