jazzyisj/unavailable-entities-sensor

Monitoring only wanted entities

Closed this issue · 6 comments

Hi and many thanks for this package! However, as on previous one, i again wonder if you can add a package, which would only monitor entites i want, and not all of them? I'd like to monitor only my wifi devices, so to monitor only "sensor.wifi_signal_strength_device". It would be pretty painfull if i'd have to enter all my entities in ignored list, since there are over 200 of them.

Currently i have your old "thing" and all my monitored entities in "grooup.included_entities" (around 15 of them). Would it be possible to use my current group or must i enter these entities in new package yaml?

I'm not so skilled in this language, so i can't do it myself...

Thanks!

EDIT: stupid me... i found your change at the very end. Only thing i don't know (yet) is WHERE to put monitored entities?

If you are using an older version of the sensor, I recommend changing to this one. Just copy the ignored_entities group from the old package and paste it into this new one, then delete the old package.

You can also have more than one version of a sensor of this type in your config. Just add them to this package file and make sure you give the sensors different names (eg unavailable_entities, unavailable_wifi_devices, etc. etc). They can all share the same ignored_entities group so you only need to declare this group once and it will be available anywhere you need to use it.

    template:
      - sensor:
          - unique_id: unavailable_entities
            name: 'Unavailable Entities'
            unit_of_measurement: entities
            state: >
              {% if state_attr('sensor.unavailable_entities','entities') != none %}
                {{ state_attr('sensor.unavailable_entities','entities')|count }}
              {% endif %}
            attributes:
              entities: >
                {% set ignore_sec = 60 %}
                {% set ignore_ts = (now().timestamp() - ignore_sec)|as_datetime %}

        ETC ETC
    
          - unique_id: unavailable_wifi_devices
            name: 'Unavailable Wifi Devices'
            unit_of_measurement: entities
            state: >
              {% if state_attr('sensor.unavailable_wifi_devices','entities') != none %}
                {{ state_attr('sensor.unavailable_wifi_devices','entities')|count }}
              {% endif %}
            attributes:
              entities: >
                {% set ignore_sec = 60 %}
                {% set ignore_ts = (now().timestamp() - ignore_sec)|as_datetime %}
                
        ETC ETC

To monitor only sensor entities who's entity_id match a string you can first narrow it down to the sensor domain, then select only entities that contain the string "wifi_signal_strength_" in their entity_id.

{% set ignore_ts = (now().timestamp() - ignore_sec)|as_datetime %}
{{ states
  |selectattr('domain','eq','sensor')
  |selectattr('entity_id','match','wifi_signal_strength_')
  |rejectattr('entity_id','in',state_attr('group.ignored_entities','entity_id'))
  |rejectattr('last_changed','ge',ignore_ts)
  |selectattr('state','in',['unavailable','unknown','none'])|map(attribute='entity_id')|list }}

If you need to match MORE than one string it gets a little more complicated because you have to iterate the states object for each string you want a match for then add the lists together.

To match on a name instead of an entity_id you can change this line

|selectattr('entity_id','match','wifi_signal_strength_')

to something like this

|selectattr('name','match','WIFI Signal Strength')

Don't forget to restart Home Assistant!

Thanks for your reply!

So, if i understand correct above example should monitor all entities , beginning with "sensor.wifi_signal_strength_", correct? Ok, except ones in ignored group, of course.

However, sensor like this:

template:
  - sensor:
      - unique_id: unavailable_wifi_devices
        name: "Unavailable Wifi Devices"
        unit_of_measurement: entities
        state: >
          {% if state_attr('sensor.unavailable_wifi_devices','entities') != none %}
            {{ state_attr('sensor.unavailable_wifi_devices','entities')|count }}
          {% endif %}
        attributes:
          entities: >
            {% set ignore_seconds = 60 %} 
            {% set ignore_ts = (now().timestamp() - ignore_sec)|as_datetime %}
            {{ states
              |selectattr('domain','eq','sensor')
              |selectattr('entity_id','match','wifi_')
              |rejectattr('entity_id','in',state_attr('group.ignored_entities','entity_id'))
              |rejectattr('last_changed','ge',ignore_ts)
              |selectattr('state','in',['unavailable','unknown','none'])|map(attribute='entity_id')|list }}

gives me a blank state, in developer tools attribute "entities" is "null", altohugh i do have a number of sensors, starting with "sensor.wifi_...": like

  • sensor.wifi_room_pavel
  • sensor.wifi_garage
  • sensor.wifi_outdoor

From old sensor experience i believe that when all wifi.... sensors are ok it should show zero.

In ESPHome i always name my wifi signal strength sensor like this, only last part is named after location.
Where did i "mess up"?

EDIT: ah....i found an error in variable "ignore_sec" instead "ignore_seconds". Now i get zero value, which means that although entity works it founds none unavailable enitites. But they are...

Maybe a solution like yours, but only change "ignored" to "monitored" would be easier...if it's possible

YEAH!! Works!
sorry for double post, but i think i found a solution and it would be more readable in a separate post...

Thank god for developer tools and "template" section - i did some tinkering and tryouts, and got it to work.

For above "script" to work i've had to change
|selectattr('entity_id','match','wifi_')
into:
|selectattr('entity_id','match','sensor.wifi_')
Now i get all my "sensor.wifi_...." entities inside and i can ignore those, which are "occasional" (like power switches...).
Now i have to get to work my warning to my phone, but that won't be a problem.

Thank god for developer tools and "template" section - i did some tinkering and tryouts, and got it to work.

i found an error in variable "ignore_sec" instead "ignore_seconds"

@Protoncek
You must have caught me in between commits. I did change that variable name. Glad you got it figured out!

Another user discovered a bug you should also fix. I changed something at one point and forgot to change one thing.

Change |rejectattr('last_changed','lt',ignore_ts) to |rejectattr('last_changed','ge',ignore_ts)

Note: I edited my replies to you as well as your comment that contained the bug so nobody else copies it by mistake.

Thanks! I'll correct it. Although it worked before, too... or maybe i just didn't came across this bug yet...