How to monitor/track unavailable devices

Hi

Looks like after two years of service my Xiaomi Zigbee devices are starting to die out because batteries run out.

Currently I do not have any monitoring/daily reports of unavailable devices.

Anyone made such and would like to share with us?

I have two automations, one that monitors Seaverā€™s device battery to notify me when they are getting low. The other notifies me if any of the devices are reported ā€˜deadā€™ on the Seaverā€™s network.

I can see that you enter specific entity ids. I was think about checking liveliness of every entity in hass without adding it by name in the automation.
Some for loop or sth.

This way every new device will be automatically monitored.

Ok. I came up with something like:

- alias: Alert when entity is unavailable
  trigger:
    platform: time
    at: '21:00:00'
  condition:
    condition: template
    value_template: >
      {%- for item in states -%}
        {%- if is_state('item', 'unavailable') -%} {{ True }} {%- endif -%}
      {%- endfor -%}
  action:
  - service: notify.olivia
    data_template:
      message: >
        {%- for item in states -%}
          {%- if is_state('item', 'unavailable') -%} {{ item.attributes.friendly_name }} unavailable  {%- endif -%} A
        {%- endfor -%}

But it does not work. I always get message:

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- alias: Alert when entity is unavailable
  trigger:
    platform: time
    at: '21:00:00'
  condition:
    condition: template
    value_template: >
      {% for item in states %}
        {% if item.state == 'unavailable' %} true
        {% else %} false {% endif %} 
      {% endfor %}
  action:
    service: notify.olivia
    data_template: >
      {% for item in states %}
        {% if item.state == 'unavailable' %}
          {{ item.attributes.friendly_name }} is unavailable 
        {% endif %}
      {% endfor %} 

Iā€™m not mega convinced that the condition is right, but the message in the action section should be bang on.

Thanks! Looks like it working on manual test. I will let it run for couple days and see if it is stable :-).

1 Like

Looks like it never triggers. I removed the condition and trigger is working based on time.

So the problem is the condition:

  condition:
    condition: template
    value_template: >
      {% for item in states %}
        {% if item.state == 'unavailable' %} true
        {% else %} false {% endif %} 
      {% endfor %}

It never works, even though I have unavailable devices.

Go to the states tab. Is the state ā€˜unavailableā€™ or ā€˜Unavailableā€™?

Also, when I put this in the template dev tool it provides an ouptut for each device, not a single true/false. I think the condition needs a single true/false to process.

unavailable -> it is small letters, so this one is OK.

I think you are right about single true/false. I am searching how to do it.

If youā€™re looking for a list of your sensors that have a status of ā€˜unavailableā€™ this would do it.

{{ states.sensor | selectattr('state', 'eq', 'unavailable') 
                 | map(attribute='name')
                 | list }}
3 Likes

@anon43302295, @Oliviakrk

I think this condition would do what you guys were looking for. Personally I would put this into the trigger so youā€™d get a notification right away instead of waiting for a specific time but thatā€™s me.

condition: template
value_template: >
  {% set unavails = states.sensor | selectattr('state', 'eq', 'unavailable') 
                 | map(attribute='name')
                 | list
                 | length  %}
  {{ unavails > 0 }}
6 Likes

It works!

Thanks.

I made two automations: one to inform me a device has just died and a second one as a daily report.

Glad I could help. I had some time this afternoon so I played with this a bit more and came up with a nifty little unavailable sensor senor. Thought you might find it interesting.

4 Likes

Is this the proper syntax, using the template as the Trigger?

- alias: Alert when entity is unavailable
  trigger:
    platform: template
    value_template: "{% for item in states %}\n  {% if item.state == 'unavailable'\
      \ %} true\n  {% else %} false {% endif %} \n{% endfor %}\n"
  condition: []
  action:
  - service: notify.mobile_app_snote8
    data_template:
      message: "{%- for item in states -%}\n  {%- if is_state('item', 'unavailable')\
        \ -%} {{ item.attributes.friendly_name }} unavailable  {%- endif -%} A\n{%-\
        \ endfor -%}"

I have created/added this yaml file, but how do I use this in an automation? Will the Trigger be this sensor?

I think that gist is an older version of the sensor. There is another thread where there was a collectively improved version. I would use this instead of my gist code linked here. If you have questions I would ask them in that thread, it was way more active than this one.

As for your question, you can use the state of this sensor to trigger an automation as you would any other sensor. Since the state of this sensor is a numeric value with anything other than 0 meaning there is an unavailable sensor you can just use a numeric value trigger.

Thank you!

Anchio ho lo stesso problema, qualcuno ha risolto ? Grazie

Since this thread is so long, Iā€™m compiled what you need to do in a single blog post. Hope it saves you guys some time!

3 Likes

Great guide, many thanks!
If i understand correct this monitors all entities except ignored ones, right? Right now i have my set up reversed: i monitor only entities i want and i put those ones in ā€œincluded_entitiesā€ group. Maybe it would be a good idea to add this guide, too (if you have time, sureā€¦) Of course, both versions have good and bad sideā€¦ but i prefer this way, since i always ā€œtinkerā€ with HA and modules, so i have pretty much constantly some offline entities and very often different ones. So i would had to add exclusions constantlyā€¦