Workaround for Unifi restarts / Device Tracking

Dear all

Once in a while, the Unifi Integration restarts. As I have an automation, which triggers at

trigger: numeric_state
entity_id:
  - zone.home
above: 0

I have the situation that an unknown state fires the event, too.
Previously I made a group of persons and triggered on the group’s state. That worked fine, but I had to manually update the group when people join my home. So I switched to the numeric state of a zone above. (Just for me and my personal satisfaction without hardcodings :wink: )

Do you have any Ideas how I could prevent unknown state firing?

1 Like

WHICH state. Be specific.

Show your code where you want to do this so we’re not guessing. It’s easy to avoid unknown and unavailable we just need to see what you’re trying to avoid it for specifically so we know exact syntax etc.

Rn you gave us enough to guess…

It’s straight forward, I use

  # Template Sensor for updating persons, even, if they're unavailable | unknown
  - sensor:
      - name: "Zone Home People Available"
        unique_id: zone_home_people_available
        icon: mdi:home
        state: >
          {% set people = expand('zone.home') %}
          {% set total = states.person | list | length %}
          {% set valid = people
            | selectattr('state', 'eq', 'home')
            | list
            | length %}
          {% set invalid = states.person
            | selectattr('state', 'in', ['unknown', 'unavailable'])
            | list
            | length %}

          {# If all tracked persons are invalid (e.g. Unifi reboot) -> keep last value #}
          {% if invalid == total %}
            {{ states('sensor.zone_home_people_available') | int(0) }}
          {# If no one is home -> 0 #}
          {% elif valid == 0 %}
            0
          {# Otherwise use current valid count #}
          {% else %}
            {{ valid | int(0) }}
          {% endif %}

        attributes:
          at_home: >
            {{ expand('zone.home')
              | selectattr('state', 'eq', 'home')
              | map(attribute='name')
              | list }}
          away: >
            {{ states.person
              | selectattr('state', 'eq', 'not_home')
              | map(attribute='name')
              | list }}
          unknown: >
            {{ states.person
              | selectattr('state', 'eq', 'unknown')
              | map(attribute='name')
              | list }}
          unavailable: >
            {{ states.person
              | selectattr('state', 'eq', 'unavailable')
              | map(attribute='name')
              | list }}

Update 20260210: there was an issue with the attribute order - fixed

Btw, this is also important as the unifi integration has cold backup configured! So, you’re leaving home, whenever HA does a new backup.