Automation on state change of two device trackers

This can’t be hard but I haven’t quite figured out how to tackle this so I am asking for help to point me in the right direction.

I want a lovelace switch to be flicked “OFF” if two device trackers report “away” (AND) and turned “ON” when either one of the devicetrackers switches to “home” (OR).

Should this best be a time based trigger checking like every two minutes, or a template trigger or…?

State trigger would be better.

The OR case is easy as this is the native mode for multiple triggers. If any of the triggers are true the automation will trigger. Simply add the two trackers as triggers and check for state: “home”.

The AND case can use the same trigger set up (in a seperate automation) but looking for state: ‘not_home”. Then add two state conditions. Both trackers must be ‘not_home’ for the automation to run. Conditions are AND by default.

1 Like
- id: either_home
  alias: 'Either Home'
  trigger:
    platform: state
      - entity_id: device_tracker.tracker1
      - entity_id: device_tracker.tracker2
    to: 'home'
  action:
    ...

- id: both_away
  alias: 'Both Away'
  trigger:
    platform: state
      - entity_id: device_tracker.tracker1
      - entity_id: device_tracker.tracker2
    to: 'not_home'
  condition:
    - condition: state
      entity_id: device_tracker.tracker1
      state: 'not_home'
    - condition: state
      entity_id: device_tracker.tracker2
      state: 'not_home'
  action:
    ...
1 Like

Might want to remove those invalid quote characters

Heh. Just spotted them and did so. That’s the trouble using an iPad in bed. I actually had to copy and paste the right type. The single quotes the iPad uses are the wrong type.

1 Like

I hate coding on tablets!

I normally wouldn’t, but can’t sleep and bored. Even remoting into my PC to do it isn’t much easier on this thing.

Thanks l @tom_l!

I added this to the automation.yaml file (I am on HASS.IO). I get one error message in the user interface because more than one triggers aren’t allowed yet (in the GUI) but I guess I can ignore that.

Will now try and see if it does what it should! :hushed:

Ah, HASS.IO does not seem to like it…

Failed config
  automation: 
    - Invalid config for [automation]: required key not provided @ data['trigger'][0]['platform']. Got None. (See /config/configuration.yaml, line 158). Please check the docs at https://home-assistant.io/components/automation/

This is my automations.yaml

- id: '1542118454340'
  alias: Bilderrahmen an, wenn einer zuhause ist
  trigger:
  - entity_id: device_tracker.tanjaiphone6
  - entity_id: device_tracker.wolfgangx
    platform: state
    to: home
  condition: []
  action:
  - data:
      entity_id: switch.bilderrahmen
    service: switch.turn_on
- id: '1542118682597'
  alias: Bilderrahmen aus, wenn keiner zuhause ist
  trigger:
  - entity_id: device_tracker.tanjaiphone6
  - entity_id: device_tracker.wolfgangx
    platform: state
    to: not_home
  condition:
    - condition: state
      entity_id: device_tracker.tanjaiphone6
      state: 'not_home'
    - condition: state
      entity_id: device_tracker.wolfgangx
      state: 'not_home'
  action:
  - data:
      entity_id: switch.bilderrahmen
    service: switch.turn_off

I stuffed up. Try altering the triggers to look like this for both automations.

- id: either_home
  alias: 'Either Home'
  trigger:
    - platform: state
      entity_id: device_tracker.tracker1
      to: 'home'
    - platform: state
      entity_id: device_tracker.tracker2
      to: 'home'
  action:

Your indentation is a bit off there.

- id: both_away
  alias: 'Both Away'
  trigger:
  - platform: state
    entity_id: 
    - device_tracker.9c2ea1e18889
    - device_tracker.18f0e4eb03ff
    to: 'not_home'
  condition:
  - condition: state
    entity_id: device_tracker.9c2ea1e18889
    state: 'not_home'
  - condition: state
    entity_id: device_tracker.18f0e4eb03ff
    state: 'not_home'
  action:
  - service: switch.turn_off
    entity_id:
    - switch.plug_158d00011997c5

i did in this way

1 Like

@sabath21: Are you using HASS.IO or Home Assistant?

Home Assistant. I haven’t tried hass.io yet

Your indentation is a bit off there.

Do you mean this section:

  - data:
      entity_id: switch.bilderrahmen
    service: switch.turn_off

It was created by HASS.IO and it actually works. Yes, but it looks indeed strange…

So I took out the second device to test the general functionality and that works. Now let’s see how I can get the second device in there in HASS.IO.

It works! :grinning:

So, in HASS.IO you have to go through the GUI, you can’t just edit the automations.yaml file.

But in the end, this is how it looks, the modifications are very slight.

- id: '1542118454340'
  alias: Bilderrahmen an, wenn einer zuhause ist
  trigger:
  - entity_id: device_tracker.wolfgangx
    platform: state
    to: home
  - entity_id: device_tracker.tanjaiphone6
    platform: state
    to: home
  condition: []
  action:
  - data:
      entity_id: switch.bilderrahmen
    service: switch.turn_on
- id: '1542118682597'
  alias: Bilderrahmen aus, wenn keiner zuhause ist
  trigger:
  - entity_id: device_tracker.wolfgangx
    platform: state
    to: not_home
  - entity_id: device_tracker.tanjaiphone6
    platform: state
    to: not_home
  condition:
  - condition: state
    entity_id: device_tracker.tanjaiphone6
    state: not_home
  - condition: state
    entity_id: device_tracker.wolfgangx
    state: not_home
  action:
  - data:
      entity_id: switch.bilderrahmen
    service: switch.turn_off

Thanks to all helpers!

Silly question:
Why not just put both trackers in a group and have the group state change trigger the actions, i.e. no fussing about with all the conditions?

Or am I missing something important here?

And you really just can’t simply edit the automation code in the respective yaml-file in hass.io?

Why not just put both trackers in a group and have the group state change trigger the actions, i.e. no fussing about with all the conditions?

That would probably work just as well, yes.

And you really just can’t simply edit the automation code in the respective yaml-file in hass.io?

Well, maybe you can and I just made a mistake while editing. The thing is if you know what to do in the GUI, it’s really super easy.

I’m not using hass.io so I’m just trying to understand.

And I tried to use the GUI to manage my automations waaay back when it was introduced - it seriously s%$@#& up most of them at that point in time. So I kept doing it manually and never tried again :wink: