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 tho 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:
      - name: "Zone Home People Available"
        state: >
          {% set people = expand('zone.home') %}
          {% set valid = people
             | selectattr('state', 'eq', 'home')
             | list
             | length %}
          {% set invalid = people
             | selectattr('state', 'in', ['unknown', 'unavailable'])
             | list
             | length %}
          
          {# If all people are invalid, keep last known value #}
          {% if invalid == people | list | length %}
            {{ states('sensor.zone_home_people_available') | int(0) }}
          {% else %}
            {{ valid }}
          {% endif %}
        attributes:
          at_home: >
            {{ expand('zone.home')
               | selectattr('state', 'eq', 'home')
               | map(attribute='name')
               | list }}
          away: >
            {{ states.person
               | rejectattr('entity_id', 'in', expand('zone.home') | map(attribute='entity_id') | list)
               | 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 }}