Blueprint: Notify when Zigbee/Zwave/any battery device has gone offline

Unavailable Sensors: 
    <template TemplateState(<state sensor.african_violet_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.625224-05:00>)>
<template TemplateState(<state sensor.areca_palm_desk_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.624655-05:00>)>
<template TemplateState(<state sensor.areca_palm_window_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.623744-05:00>)>
<template TemplateState(<state sensor.cactus_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.624312-05:00>)>
<template TemplateState(<state sensor.govee_14241_water_sensor_battery_level=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Govee-14241 battery level, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.626411-05:00>)>
<template TemplateState(<state sensor.govee_14275_water_sensor_battery_level=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Govee-14275 battery level, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.626756-05:00>)>
<template TemplateState(<state sensor.govee_50227_water_sensor_battery_level=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Govee-50227 battery level, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.627110-05:00>)>
<template TemplateState(<state sensor.govee_50303_water_sensor_battery_level=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Govee-50303 battery level, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.627450-05:00>)>
<template TemplateState(<state sensor.jade_plant_floor_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.625403-05:00>)>
<template TemplateState(<state sensor.jade_plant_lower_shelf_left_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.637095-05:00>)>
<template TemplateState(<state sensor.jade_plant_lower_shelf_right_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.625744-05:00>)>
<template TemplateState(<state sensor.jade_plant_top_shelf_left_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.625047-05:00>)>
<template TemplateState(<state sensor.jade_plant_top_shelf_right_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.624486-05:00>)>
<template TemplateState(<state sensor.jade_plant_window_left_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.624127-05:00>)>
<template TemplateState(<state sensor.jade_plant_window_right_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.625574-05:00>)>
<template TemplateState(<state sensor.living_room_humidifier_hygrometer_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Living Room Humidifier Hygrometer Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.620261-05:00>)>
<template TemplateState(<state sensor.spider_plant_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.624867-05:00>)>
<template TemplateState(<state sensor.thanksgiving_cactus_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.637992-05:00>)>

Looks like mostly my Xiaomi BLE plant sensors, and a few Govee water leak sensors I may have added through MQTT. Remind me: can I exclude with your blueprint? If so I can exclude the plants (because they’re otherwise reporting just fine, one just blew up my phone telling me it needed water!) …not sure about the Govee/MQTT devices. I think I’ve got them pulled in now via HomeKit, so I don’t think I need the MQTT variety.

I have excluded the plant sensors, and outright removed the Govee/MQTT devices. It’s set to run again in a bit over 20 minutes, so I’ll let you know how that goes.

I must have missed plants when I excluded, though I swear I counted 11.

Unavailable Sensors: 
    <template TemplateState(<state sensor.african_violet_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.625224-05:00>)>
<template TemplateState(<state sensor.areca_palm_desk_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.624655-05:00>)>
<template TemplateState(<state sensor.areca_palm_window_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.623744-05:00>)>
<template TemplateState(<state sensor.cactus_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.624312-05:00>)>
<template TemplateState(<state sensor.jade_plant_floor_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.625403-05:00>)>
<template TemplateState(<state sensor.jade_plant_lower_shelf_left_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.637095-05:00>)>
<template TemplateState(<state sensor.jade_plant_lower_shelf_right_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.625744-05:00>)>
<template TemplateState(<state sensor.jade_plant_top_shelf_left_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.625047-05:00>)>
<template TemplateState(<state sensor.jade_plant_top_shelf_right_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.624486-05:00>)>
<template TemplateState(<state sensor.jade_plant_window_left_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.624127-05:00>)>
<template TemplateState(<state sensor.jade_plant_window_right_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.625574-05:00>)>
<template TemplateState(<state sensor.living_room_humidifier_hygrometer_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Living Room Humidifier Hygrometer Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.620261-05:00>)>
<template TemplateState(<state sensor.spider_plant_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.624867-05:00>)>
<template TemplateState(<state sensor.thanksgiving_cactus_battery=unavailable; restored=True, state_class=measurement, device_class=battery, friendly_name=Battery, supported_features=0, unit_of_measurement=% @ 2023-02-22T20:22:15.637992-05:00>)

Edited, about 12 hours later: Ah crud. I think I missed the two areca palms! That explains it. Now I just need to smack around the tiny hygrometer to make it behave. Thanks for all the feedback!

1 Like

No I don’t. I use deCONZ. Before many others search their back off, better specify for which integration this can be expected (ZHA? ZB2MQTT?).

Good point! I have updated the document to specify that those instructions are for the built in Home Assistant Zigbee integration (ZHA) - those using an alternative integration (Z2M/DeConz) will have to find their own instructions, as I personally do not have either of those integrations.

If anyone reading this is running Z2M or DeConz, and knows how to adjust the “unavailable” timeout for those two integrations, if you can provide instructions for that I will edit the original post to contain those instructions.

I am using Z2M. You go to settings, and in that first settings options, select availability. Select availability (advaced) and then you have options to change the amount of time after which an active device will be marked as offline in minutes. Default is 25 hours (1500 minutes).

for me this code sends in notification as “unavailable” a lot more than just the battery powered devices.
but the debug code correctly filters and displays the only battery powered device that is offline.

so… what could be different between the debug code and the code that is run in automation?

image

Debug code output:

Unavailable Sensors: 
    <template TemplateState(<state sensor.tradfri_button1_battery=unavailable; state_class=measurement, unit_of_measurement=%, device_class=battery, friendly_name=Tradfri_Button1 Battery @ 2023-03-06T00:30:13.892090+02:00>)>
Unavailable binary sensors:

After some debugging, I noticed that the blueprint contains a bit more than the debug code provided. And the debug code is not really nice for printing in the template page.

The blueprint checks the sensors, the binary_sensors and then the switches. And it seems I have a number of offline switches → thus the long notification.
I just need to add quite a few switches in the exclusion list.

a more complete debug code that can be put in Developer tools->Template to understand what is going on::

Checking sensors...
{%- set result = namespace(sensors=[]) %}
    {%- for state in states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') %}
      {%- if "=unavailable" in state | string  %}
        {%- set result.sensors = result.sensors + [state] %}
      {%- endif %}
    {%- endfor %}
    Unavailable Sensors: 
    {{result.sensors|join('\n')}}

    Checking binary_sensors...
    {%- set result = namespace(sensors=[]) %}
    {%- for state in states.binary_sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') %}
      {%- if "unavailable" in state | string %}
        {%- set result.sensors = result.sensors + [state] %}
      {%- endif %}
    {%- endfor %}
    Unavailable binary sensors: 
    {{result.sensors|join('\n')}}

    Checking switches...
{%- set result = namespace(sensors=[]) %}
    {%- for state in states.switch | selectattr('state','eq','unavailable')%}
      {%- set result.sensors= result.sensors + [state] %}
    {%- endfor %}
    Unavailable switches:
    {{result.sensors|join('\n')}}
1 Like

Hi, It’s a great blueprint. Would it be possible to change it to run several times a day like on an interval. ? Would like to know as soon as possible if my ZHA devices goes offline.
Thanks.

well…yes, but since time_pattern has a lot of parameters (hour, minute, second, etc…) it would require adding as input in the blueprint a lot more fields.

Still, you can do it if you modify code of the blueprint on your setup.
there should be a yaml file in blueprints/automation/Tahutipai. In that file you need to replace:

trigger:
- platform: time
  at: !input 'time'

with something like:

trigger:
- platform: time_pattern
  minutes: "/5"

and it should trigger every 5 minutes…

This is great—thank you! I’m using the beta blueprint and I wanted to get alerts for a “mains connected” Sonoff S31 power plug, but it’s not showing up in the list of devices to exclude so I’m not sure that the beta blueprint is seeing it. It also seems to be including some automations and devices that I excluded. Any ideas?

Edit; I added entities and removed some old switches that looked like they were named similar to automations. I think the only issue is making sure the Sonoff S31 switches are included.

How would I change this to run at regular intervals?

If you are using ZHA then the following template sensor can provide also the offline mains powered devices:

{% set str = states.button | selectattr('attributes.device_class', 'eq', 'identify') | selectattr('state', 'eq', 'unavailable') | map(attribute='attributes.friendly_name') | list | join(', ') | replace(' Identify', '') %}
{% if str | length == 0 %}
  None!
{% elif str | length < 256 %}
  {{ str }}
{% else %}
  Too many!
{% endif %}

Thank you; did you modify the blueprint to include that? If so can you share the fully modified version?

No, I’ve just created a helper (template sensor). This way the changes are instant whenever a device goes offline the sensor is updated. You can then create an automation to trigger based on the state change and send notification/do other action.

Hi, I setup to run this check every day, but it doesn’t… it seems that this check is always false

{{ sensors != '''' and (day | int == 0 or day | int == now().isoweekday()) }}

If I check this code in the template i got:

UndefinedError: ‘day’ is undefined

How can I enable the “day” sensor?

Thanks

Is there any way to extract the area name of the entities contained in the {{sensors}} array - I’ve tried a few template options but they all return “None”.

I’m not sure of this answers your question correctly, please let me know… I wasn’t happy that this Template retina the Sensors, and not the Device that contains the Sensor(s).

For example, if a Device contains multiple Sensors, I don’t want the multiple Sensors being reported as being offline, I just want the single Device to be reported, as that is cleaner and simpler.

I’ve written a new version of this blueprint that does this. Also tested it for ZHA and Z2M. Also wanted it to report offline switchs (smart plugs) as well as all battery devices.

I’m been testing it, it works great, I’ll post it soon after a little more testing.

Does that answer your question?

I am also one of those that are more interested in monitoring ZHA & ZWave mains powered devices as having one (or more) of those going offline causes a disruption in the force, errr mesh :slight_smile:

I’ve been trying to get this Unavailable-unknown entities blueprint to report both ZHA & ZWave_JS devices that go unavailable but only seem to get one or the other.

Hi @itcsburnett & @DJBenson ,

I’ve just put up the new blueprint here for you.

I would appreciate if you can try it out, and let me know how it works for you.

Note: This should work for all battery devices, ZHA, Z2M, ZWave and all “Smart Plugs” (Switches) which as you know act as Wireless Repeaters.

This blueprint does not currently work for mains powered devices which are not “Switches”. For example, it would not work for a dedicated Zigbee repeater device that is not also a switch/smart plug. (I don’t have one of those to test with).

If you have any mains-powered devices that this blueprint doesn’t work for, I would be happy to collobarate and enhance it so it does work for your devices.

Let me know how this goes for you.

Cheers

1 Like

Hi everyone,

I consider this blueprint deprecated, click here for an updated and improved version

There are a few differences. The main updated is only Devices will be reported, not Sensors. This makes more sense, because if one device has 5 sensors, you don’t need to know all 5 sensors are offline - you only need to know the device is offline.

Also the new version is confirmed working with Zigbee2Mqtt, ZHA, also works with your smartplugs - not only battery devices.

Cheers.

1 Like