How to check if person is in a zone inside a template?

Hello,
I have an automation that sends a notification when the person enters a zone. I want to have a condition to make sure they’re not inside another zone (I have 2 zones, where 1 zone is inside the other. So I’m trying to see if they’re outside the inner zone)

The problem is I have several entities that trigger this automation, so I can’t simply use a not condition with a zone condition (since I can’t seem to use a {{ trigger.entity_id }} as the input to a zone condition.

Note that my zones I’m checking against here are passive, because I only want them for automation. I have my normal ‘home’ zone which is active, and much smaller, but i’m specifically concerned with how to use the template system to check/compare zones.

I tried the below, but it fails to get interpreted.

- alias: 'Alert for people within 5 minutes (notification)'
  trigger:
  - platform: zone
    entity_id:
      - person.chance
      - person.dennis
      - person.david
      - person.lee_valdez
      - person.victoria
      - person.cole
    zone: zone.big_home_radius
    event: enter
  condition:
    - not:
        - condition: zone
          entity_id: "{{ trigger.entity_id }}"
          zone: zone.small_home_radius
  action:
  - service: notify.notify
    data:
      message: 'This is a warning that {{ trigger.from_state.attributes.friendly_name }} will arrive in less than 5 minutes.'
      title: Guest Close

Using a value template, It seems like it can only do a state comparison, and not check zones, which is not what I need.

condition:
    - condition: template
      value_template: "{{ not is_state( trigger.entity_id, 'zone.small_home_radius') }}"

Is there a value template syntax that can check if an entity is inside a zone??

Also, where do I see these zones

1 Like

Mmm… When a person enter the “big_radius”, whether from “outside” or from “small_radius”, they cannot be also be in the “small_radius”, so the condition seems unnecessary, here.

The following returns a list of the person entities in the zone state_attr('zone.small_home_radius', 'persons'). In your condition you can check to see if the person entity that triggered the automation is in that list:

condition:
    - condition: template
      value_template: "{{ trigger.entity_id not in state_attr('zone.small_home_radius', 'persons') }}"

Also, you should take a look at the Proximity integration. It is a great built-in tool for cases like this, but it doesn’t get the recognition that it deserves.

This is helpful! Thank you.

I tested it and it doesn’t appear to work for passive zones.

The proximity integration I did not know about. Definitely taking a look at that.

Now for a 2nd question pertaining to this.

is there a way to check how long the user has NOT been in a zone? I.e. if a person leaves the zone, I would only want another notification if that user has been outside of the zone for 2 hours.

- condition: template
  value_template: '{{ as_timestamp(trigger.entity_id) + 7200 < as_timestamp(now()) }}'

I know that this won’t work, but i’m not sure how to even begin to tackle looking at how long a person has NOT been in a zone. If the zone is home/not home, then it’s easy to just look at the last state change, but in the case of these passive zones, I don’t even know if it’s possible.

So after testing this, apparently passive zones don’t list the people that are in the zone…

Seems like a bug. Don’t know why a passive zone wouldn’t have the people in it on the persons attribute.

There is a logic to not having an active persons: attribute for passive zones since the zones aren’t reflected in the person entities’ state. If they were like an active zone, would the person be counted in both the active and passive zone? The combination does seem to seriously limit how useful they could be. With their current behavior, do they have any utility beyond use in a Zone trigger?

I can’t think of an unconvoluted way to do it for 6 people. Normally I would say set up a template binary sensor… but, with the limitations you have brought up, it would require setting up an alternate method of tracking the zone which includes passive zones.

I feel like a passive zone should contain all users that are within it, even if that zone is not reflected in the person entity.

I think it would make sense to have a user be listed in all zones that they are physically inside, even if that means more than 1, but only active zones are reflected in the person entity.

The way it’s implemented now does seem to make passive zones only useful for triggering on enter/exit and not trying to determine if a person actively inside.

Shame there’s no easy way to figure out how long someone was in a zone, but I’m not surprised. You can do this with the person entity pretty easily, but that only works with active zones :frowning:

I raised this as an issut to the HASS Core github because I believe that passive zones SHOULD infact update their persons attribute to list all people inside that zone, even if passive.