Sensor template that only updates for specific locations

I’m trying to create a series of people-location sensor templates that only reflect a specific set of zones.

I have 8 templates (1 per person) that, as attributes, includes

  1. last_location
  2. new_location

This was easy.

Some of the locations (or HA zones) I track are:

  • home
  • near_home
  • kids home 1
  • kids home 2
  • other city 1
  • other city 2
  • many others

On my dashboard I want to display interactions with “home”. Thus I want my templates to only update when either last_location or new_location become “home” or “near_home”

Here is an example of one of my existing templates: (yes, I know the now() trigger isn;t particularly efficient but I’m experimenting right now)

  - trigger:
      - platform: state
        entity_id: person.john_smith
        to:
    sensor:
      - name: person john smith location template
        state:  "{{ now() }}" 
        attributes: 
          display_name_for_table: "John Smith"
          from_location {{state_attr('sensor.person_john_smith_location_template','new_location')}}"
          new_location: "{{ states('person.john_smith').replace('not_home', 'Away')}}"
          short_name: "John"
          master_sort_value: 5

Is there an elegant way to get this to update just for the specific locations (zones) I want - or will I end of with lots of nested logic where I assign values to my attributes?

Some things I need to consider:

I was looking at triggering on a change in the state of the person (which should indicate a new_location). That could be anywhere (I can’t trust that it would be an existing zone as someone might turn on their cell phone while in the home so from_location could be unknown).

Also, someone could turn off their cell phone just before leaving the house, so I wouldn’t get a new location update when they leave.

Two of the visitors use apple phones, and these seem to always stop working in background after a while (even if back ground processing is turned on). I’m told this is a long standing apple problem so I just have to live with that.

The use case for all this:
I travel a lot, and often for many months at a time. In general HA does a wonderful job of keeping my house in order - but I’d still like the human touch of someone walking through once a week (as would my insurance company)

So, I ask friends and family members to check on my house. I give them all the HA companion app (or at the very least track them through my router via their phones mac address) I ask that my house be checked at least once a week. Because they have the HA app, when they arrive it’s easy for them to unlock doors from their phones. In addition, HA will adjust HVAC and lights as soon as they enter the near home zone. In exchange for HA getting the house ready for their visit - I get to know they are there.

The templates above serve as the data for a flex-table where I can see the last interaction each visitor had with my house. In general the visitors go to the house on a set schedule - and they all share a whatsapp group to share findings issues etc - but they would also like to be able to look at the table and see when someone was last at the house to help them plan accordingly. My visitors have their own lives (?) and may not always be able to visit when scheduled - so this table would be the best version of the truth.

Right now that table is only correct when a visitor is at the house. Once they leave and travel to multiple other zones, I will have no idea as to when they last interacted with the house (well I can look at the history but that isn’t end-user friendly)…

Thoughts??

thanks,
Ken

I’m not sure I’m following all of this. If you merely wanted a historical list of people who have been at your house, you could do that with a single template sensor. You could have an attribute containing a list of dictionaries; each dictionary would have a person’s name and the last time they were at the house. This would be a trigger-based template sensor that triggers when the persons attribute of zone.home changes. You could also add triggers for other zones if you wanted those in your list too.

I assume I’m misunderstanding something because you seem to be trying to do something much more complex.

There are a couple way to accomplish that. One option would be to use two triggers as follows:

  - trigger:
      - platform: state
        entity_id: person.john_smith
        to:
          - home
          - near_home
      - platform: state
        entity_id: person.john_smith
        from:
          - home
          - near_home
    sensor:
      - name: person john smith location template
        state:  "{{ now() }}" 
        attributes: 
          display_name_for_table: "John Smith"
          from_location: "{{ (trigger.from_state.state).replace('not_home', 'Away') }}"
          new_location: "{{ (trigger.to_state.state).replace('not_home', 'Away') }}"
          short_name: "John"
          master_sort_value: 5

Thanks - I need to ponder if I am over complicating this :slight_smile:

Thanks. I have set this up - now I just need to wait for a visitor to make sure it works in prod. Setting the values manually though and it appears to be working right.