Multi person/device/zones tracker in single Automation

Tags: #<Tag:0x00007f7396aad8c0> #<Tag:0x00007f7396aad780> #<Tag:0x00007f7396aad618> #<Tag:0x00007f7396aad488>

Ok - so ATM I have a mass amount of automations that are going to get unweildy as I expand the setup.

I currently have 3 users, 27 zones and an automation for in/out per user so 160 automations dedicated to location tracking!!

Here is and example of an in automation:

id: 'stevenin_aberdeen_bus_station'
alias: Steven - In - Aberdeen Bus Station
description: ''
trigger:
- entity_id: device_tracker.sm_g965f
  event: enter
  platform: zone
  zone: zone.aberdeen_bus_station
condition: []
action:
- data:
    message: Steven is at Aberdeen Bus Station
    title: Steven Location
  service: notify.mobile_app_sm_g965f

out is the same but with leave and this repeats for all zones/people so yeah a lot of wasted repition of code.

What I’m looking to do:
I’d like to have a single automation that will replace all the others to fire and notify me adapting to the device/zone automagically. so when ANY person enters ANY zone a notification is sent to the chosen devices at the bottom.

I have the below but am not getting notification to either of the devices and am stuck at why so turn to those here who are far more versed at this than me for help (I’ve done a little redacting to shorten the code). Am testing by setting the device state to location_name but it’s not firing to the phones.

Hopefully this makes sense but please let me know if you need any clarification/more info.

id: 'location_test'
alias: Location testing
description: ''
trigger:
  platform: state
  entity_id:
  - person.me
  - person.wife
  - person.childerbeast
condition:
  condition: template
  value_template: >
    {% set zones = 'home',
                   state_attr('zone.place1', 'Place 1'),
                   state_attr('zone.aberdeen_bus_station', 'Aberdeen Bus Station'),
                   state_attr('zone.asda_town1', 'ASDA - Town 1'),
                   state_attr('zone.asda_town2', 'ASDA - Town 2'),
PLACES
                   state_attr('zone.fraserburgh_bus_station', 'Fraserburgh Bus Station'),
MORE PLACES
                   state_attr('zone.peterhead_bus_station', 'Peterhead Bus Station'),
EVEN MORE PLACES
                   state_attr('zone.stable', 'Stable'),
                   state_attr('zone.steven_s_town_1_office', 'Stevens town 1 Office'),
                   state_attr('zone.steven_s_town_2_office', 'Stevens town 2 Office'),
                   state_attr('zone.tesco_town3', 'Tesco - Town 3') %}
      {{ trigger.from_state and trigger.to_state and
         trigger.from_state.state != trigger.to_state.state and
         (trigger.from_state.state in zones or trigger.to_state.state in zones) }}
action:
- service: notify.mobile_app_sm_g965f
  data:
    message: '{{ trigger.to_state.friendly_name }}''s location has changed from
      {{ trigger.from_state.state }} to {{ trigger.to_state.state }}'
    title: '{{ trigger.to_state.friendly_name }}''s Location Has Changed'
- service: notify.mobile_app_vog_l29
  data:
    message: '{{ trigger.to_state.friendly_name }}''s location has changed from
      {{ trigger.from_state.state }} to {{ trigger.to_state.state }}'
    title: '{{ trigger.to_state.friendly_name }}''s Location Has Changed'```

I would say the main problem is the first part of the condition template that sets the zones variable. You need to make it a tuple or a list. E.g.:

    {% set zones = ['home',
                   state_attr('zone.place1', 'Place 1'),
                   state_attr('zone.aberdeen_bus_station', 'Aberdeen Bus Station'),
                   state_attr('zone.asda_town1', 'ASDA - Town 1'),
                   state_attr('zone.asda_town2', 'ASDA - Town 2'),
PLACES
                   state_attr('zone.fraserburgh_bus_station', 'Fraserburgh Bus Station'),
MORE PLACES
                   state_attr('zone.peterhead_bus_station', 'Peterhead Bus Station'),
EVEN MORE PLACES
                   state_attr('zone.stable', 'Stable'),
                   state_attr('zone.steven_s_town_1_office', 'Stevens town 1 Office'),
                   state_attr('zone.steven_s_town_2_office', 'Stevens town 2 Office'),
                   state_attr('zone.tesco_town3', 'Tesco - Town 3')] %}

Having said that, why are you even checking if the old & new states are one of those? Why not just check that it isn’t 'away'?

Next, you’re checking that a person changes directly from one zone to another without transitioning through 'away', which is probably not likely to happen.

Try:

condition:
  condition: template
  value_template: >
      {{ trigger.from_state is not none and
         trigger.to_state is not none and
         trigger.from_state.state != trigger.to_state.state  }}