Passive Zones not detecting people in them?

The template editor will show the count at the end. Did you copy the entire template?

Yep - there is no number at the end.

If i remove the - (so {{ ns.count }}) I get 0 outputted at the end (which I think it should be 2?)

EDIT: Yes, it seems down to the dash - without it I get a value (and can flip the < to get 2)

lol, dude the 0 is at the end, the - just removes the whitespace.

image

Anyways use this template, probably order of operation.

      {%- set zone = 'zone.my_city' %}
      {%- set radius = state_attr(zone, 'radius') / 1000 %}
      {%- set ns = namespace(count=0) %}
      {%- for person in states.person if person.attributes.latitude is defined and person.attributes.longitude is defined %}
        {%- set ns.count = ns.count + (0 if distance(zone, person.entity_id) > radius else 1) %}
      {%- endfor %}
      {{- ns.count }}
1 Like

oh, oops… learn something new every day!

Anyway - that rejigged code seems to have done the trick!

Thank you so much for your help :slight_smile:

I found this some months ago and I use it to define whether or not we are in our “home region” - if we are then automations run, if we’re not the house goes to sleep. I just spent some time trying to figure out why the region count was 0…and then I remembered than there was some dumb limitation that if you were home then you couldn’t be counted in a passive zone. Time wasted. :frowning:

But, because it bothered me (a lot) and because when we ar ein home we are in region I’ve tweaked it to include the home count - that way when someone goes from “in region” to “home” the count doesn’t change…

- sensor:
  - unique_id: wellington_region_presence_count
    name: Wellington Region Presence Count
    state: >
      {%- set zone = 'zone.wellington_region' %}
      {%- set radius = state_attr(zone, 'radius') / 1000 %}
      {%- set ns = namespace(count=0) %}
      {%- set home = states('zone.home') | int %}
      {%- for person in states.person if person.attributes.latitude is defined and person.attributes.longitude is defined %}
        {%- set ns.count = ns.count + 0 if distance(zone, person.entity_id) > radius else 1 %}
      {%- endfor %}
      {{- ns.count + home}}

Now the number only changes if someone literally leaves the region…

I know it’s an old thread but thought it might be useful…and…in my case I no longer need to test if anyone is “home” OR if anyone is “in region” as the updated “in region” now does it all!

1 Like

Thought I’d share a slightly different approach - I removed the need for a new zone and just set a radius from my home. I was looking for a sensor I could use to know we were a distance from home to set the house in to away mode.

- sensor:
    - name: Home region count
      unique_id: <some unique id>
      icon: mdi:home
      state: >
        {%- set zone = 'zone.home' %}
        {# Distance in km #}
        {%- set radius = 7.75 %}
        {%- set ns = namespace(count=0) %}
        {%- for person in states.person if person.attributes.latitude is defined and person.attributes.longitude is defined %}
        {%-   if distance(zone, person.entity_id) < radius %}
        {%-     set ns.count = ns.count + 1 %}
        {%-   endif %}
        {%- endfor %}
        {{- ns.count }}

I get the passive zones thing and use them successfully but what about the situation where you need to know if anyone is in the passive zone?

The zone has to be passive because it can overlap Home zone and cause people to appear not home

So, zone.car is a dynamically created zone based on the car device_tracker.
(Clearly the car can be at home so the zones can overlap.)

Is there any way this automation can be made to work?

  - alias: Warn If Doors Unlocked When On Holiday
    id: warn_if_doors_unlocked_when_on_holiday
    trigger:
      - platform: state
        entity_id: sensor.car_door_lock_status
        to: unlocked

    condition:
      #=== Check holiday mode is on
      - condition: state
        entity_id: binary_sensor.holiday_mode
        state: 'on'

      #=== Check nobody is at the car
      - condition: numeric_state
        entity_id: zone.car
        below: 1

    action:

Make a trigger based template sensor for people entering and leaving the zone. enter → on, leave → off.

I was hoping I’d missed something and didn’t have to create more sensors especially as I would have to remember to add to the template for any other people I might want to track in this zone at a later date.

But more importantly I’m not sure that is useful because at HA start the sensor is unknown.

template:
  - trigger:
      - platform: zone
        entity_id: person.one
        zone: zone.car

      - platform: zone
        entity_id: person.two
        zone: zone.car

    sensor:
      - name: Car Zone Occupied
        unique_id: car_zone_occupied
        state: >
          {% if trigger.event == 'enter' %}
            {% set occupants = occupants + 1 %}
          {% else %}
            {% set occupants = occupants - 1 %}
          {% endif %}
          {{ occupants }}

just add a homeassistant start trigger if you want to calculate it at start. If you want counts, you should probably just use a counter with an automation instead of a template entity.

I’ve found my way here after falling afoul of the same limitation with passive zones. I humbly suggest that where the UI says “Passive Zones can be used in automations”, it actually means “Passive Zones can be used as triggers”.

Before I switch my “near home” passive zone to an active one[1], can someone explain the reason why passive zones don’t have a count? Can a person only be in a single zone or something? How does HA choose if a person is in two active zones? Undefined?

[1] I’ll probably use a proximity distance instead.

Because that’s just how they were built when zone count changes were made.

For active zones, yes.

IIRC it chooses the closest zone.

1 Like