Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Tahutipai/971bf0e07e50ce6190e0dacd73262e2e to your computer and use it in GitHub Desktop.

Select an option

Save Tahutipai/971bf0e07e50ce6190e0dacd73262e2e to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Low battery level detection & notification for all battery sensors
blueprint:
name: Report offline zigbee/zwave/battery/smart plug devices
description: Works with Smart Plugs, ZWave, Zigbee etc (Works with ZHA & Z2M)
#By Tahutipai 2024-02-21
#Originally Based on the work of Sybx @ https://community.home-assistant.io/t/low-battery-level-detection-notification-for-all-battery-sensors/258664
#Note: This has been upgraded to report only the Device that is offline, not multiple individual sensors within one Device
domain: automation
input:
time:
name: Time to test on
description: Test is run at configured time
default: '10:00:00'
selector:
time: {}
day:
name: Weekday to test on
description: 'Test is run at configured time either everyday (0) or on a given
weekday (1: Monday ... 7: Sunday)'
default: 0
selector:
number:
min: 0.0
max: 7.0
mode: slider
step: 1.0
exclude:
name: Excluded Sensors
description: Battery sensors (e.g. smartphone) to exclude from detection. Only entities are supported, devices must be expanded!
default: {entity_id: []}
selector:
target:
entity:
device_class:
- battery
- switch
actions:
name: Actions
description: Call your notification here. {{offline_devices}} will replaced with the name of any offline devices
selector:
action: {}
source_url: https://gist.github.com/Tahutipai/971bf0e07e50ce6190e0dacd73262e2e
variables:
day: !input 'day'
exclude: !input 'exclude'
offline_devices: >-
{% set result = namespace(offline_devices=[]) %}
{% for sensor in states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') %}
{% if "unavailable" in sensor | string and not sensor.entity_id in exclude.entity_id %}
{% set result.offline_devices = result.offline_devices + [device_attr(device_id(sensor.entity_id), "name")] %}
{% endif %}
{% endfor %}
{% for binary_sensor in states.binary_sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') %}
{% if "unavailable" in binary_sensor | string and not binary_sensor.entity_id in exclude.entity_id %}
{% set result.offline_devices = result.offline_devices + [device_attr(device_id(binary_sensor.entity_id), "name")] %}
{% endif %}
{% endfor %}
{% for switch in states.switch | selectattr('state','eq','unavailable') %}
{% if switch.entity_id not in exclude.entity_id %}
{% set result.offline_devices = result.offline_devices + [device_attr(device_id(switch.entity_id), "name")] %}
{% endif %}
{% endfor %}
{{result.offline_devices|sort|unique|join('\n')}}
trigger:
- platform: time
at: !input 'time'
condition:
- '{{ offline_devices != '''' and (day | int == 0 or day | int == now().isoweekday()) }}'
action:
- choose: []
default: !input 'actions'
mode: single
@bigthrilla

bigthrilla commented Apr 1, 2023

Copy link
Copy Markdown

This has been working really well for me, thank you. Only think I customized was to put a line break between the sensors so it's easier for me to visually look through them (line 54, replace the comma space with < br >). Totally a preference thing. Thanks again for making this.

@xcdk43

xcdk43 commented Feb 2, 2024

Copy link
Copy Markdown

This is what I'm looking for, works great on a daily basis. I would like to increase the sampling from daily to some-thing more frequent hourly or every 5min

@Tahutipai

Copy link
Copy Markdown
Author

This has been working really well for me, thank you. Only think I customized was to put a line break between the sensors so it's easier for me to visually look through them (line 54, replace the comma space with < br >). Totally a preference thing. Thanks again for making this.

Great idea, thanks!

@psitem

psitem commented Feb 21, 2024

Copy link
Copy Markdown

I propose a tweak: {{result.offline_devices|join('\n')}} -> {{result.offline_devices|sort|unique|join('\\n')}}

@Tahutipai

Copy link
Copy Markdown
Author

I propose a tweak: {{result.offline_devices|join('\n')}} -> {{result.offline_devices|sort|unique|join('\\n')}}

Thanks for the suggestions. I've just added the sort (and the ability to exclude switches). Question: What does the "\n" do? (I note that when I import the blueprint, HA automatically changes \n to \\n but I don't know why....

Can you please share a use case where the unique would be useful? Is there a situation where a person may have multiple devices with the same name? (if that is possible) Or are you referring to an edge case I haven't seen myself, e.g is there a Device that might have sensors from different categories, thus erroneously appear multiple times?

@psitem

psitem commented Feb 22, 2024

Copy link
Copy Markdown

No idea why the \n -> \\n thing happens. Looking at the file in VS Code in HA, it completely messes up the formatting of that block.

Or are you referring to an edge case I haven't seen myself, e.g is there a Device that might have sensors from different categories, thus erroneously appear multiple times?

Exactly that. Most of my offline devices were repeated 4-6X due to having multiple switches and sensors.

@Tahutipai

Copy link
Copy Markdown
Author

Great thanks @psitem, have implemented your suggestions.

Ya if I look at the yaml in vscode/fileeditor/vim/cat, that block is all screwed up (appearence wise), and quite a few additional charatachers (escape'd double quotes etc). Looks like HA is running it through some type of parser.

I cleaned up the original file just now, there was some blank space at the end of a few lines, and a found a couple of tabs instead of spaces in places, then re-imported it: no change, same messy result.

If you even come across the answer to that one, do let me know. cheers!

@abrsti

abrsti commented Jun 11, 2024

Copy link
Copy Markdown

I get this error when I try to import the blueprint:

Invalid blueprint: expected str for dictionary value @ data['blueprint']['input']['exclude']['selector']['entity']['device_class']. Got None

@ppastur

ppastur commented Oct 3, 2024

Copy link
Copy Markdown

Hi, thanks for your work. I wanted to ask if anyone is using labelled to exclude from the report? I have tried but it doesn't seem to be working

@xbmcnut

xbmcnut commented Jan 6, 2025

Copy link
Copy Markdown

I'm using labels to exclude a couple of labs devices (nspanel01, Double Aqara Switch) that have been offline for a while along with the two plant sensors and the exclude does not work as shown by the screenshot.

alias: Report offline zigbee/zwave/battery/smart plug devices
description: ""
use_blueprint:
  path: Tahutipai/report_zigbee_zwave_devices_gone_offline.yaml
  input:
    actions:
      - action: notify.mobile_app_pete_s_pixel_7
        metadata: {}
        data:
          title: Offline Devices Summary
          message: The following devices are offline > {{offline_devices}}
    time: "10:00:10"
    exclude:
      entity_id: []
      label_id: excludenotify
      device_id:
        - 16d556e1da99872fe9e4976889b1de55
        - 1021fe40cce7cc64e60f3f7f004b858f

image

@Sireone

Sireone commented Jan 8, 2025

Copy link
Copy Markdown

Same here. Alexa devices/entities doesn't seem to work as it should. Label exclusion doesn't work either.

@bigthrilla

Copy link
Copy Markdown

Labels are....kind of working? Tuya devices are showing up that are mains powered as well.

@Ltek

Ltek commented Mar 13, 2025

Copy link
Copy Markdown

I made a fork here... https://gist.github.com/Ltek/0c9cecf632b9c32915130680d834bcf7

Updates are...

  • Excludes are working
  • According to my testing, its also a lot faster execution.

@marcopaganini

Copy link
Copy Markdown

I noticed that on line 57, if I don't add | selectattr('attributes.device_class', 'defined'), it will alert me for some wired devices I own. Funnily, I cannot exclude them (they don't show in the list)... Not sure if it's a bug or something with my installation.

@Ltek

Ltek commented Sep 13, 2025

Copy link
Copy Markdown

I noticed that on line 57, if I don't add | selectattr('attributes.device_class', 'defined'), it will alert me for some wired devices I own. Funnily, I cannot exclude them (they don't show in the list)... Not sure if it's a bug or something with my installation.

my version above has more capabilities and filtering available.

@marcopaganini

Copy link
Copy Markdown

I noticed that on line 57, if I don't add | selectattr('attributes.device_class', 'defined'), it will alert me for some wired devices I own. Funnily, I cannot exclude them (they don't show in the list)... Not sure if it's a bug or something with my installation.

my version above has more capabilities and filtering available.

Oh yes, I just found it and I'm already using it, thanks! I now discovered I have a device that, for some reason, will always report "unknown" in the battery... But that's another story.

Thanks for your version!

@avogadro23

avogadro23 commented Jun 19, 2026

Copy link
Copy Markdown

I'm using labels to exclude a couple of labs devices (nspanel01, Double Aqara Switch) that have been offline for a while along with the two plant sensors and the exclude does not work as shown by the screenshot.

alias: Report offline zigbee/zwave/battery/smart plug devices
description: ""
use_blueprint:
  path: Tahutipai/report_zigbee_zwave_devices_gone_offline.yaml
  input:
    actions:
      - action: notify.mobile_app_pete_s_pixel_7
        metadata: {}
        data:
          title: Offline Devices Summary
          message: The following devices are offline > {{offline_devices}}
    time: "10:00:10"
    exclude:
      entity_id: []
      label_id: excludenotify
      device_id:
        - 16d556e1da99872fe9e4976889b1de55
        - 1021fe40cce7cc64e60f3f7f004b858f

image

Exclude by label still not working for me in 2026. Great blueprint otherwise, and I really appreciate all the work you've done.

I forked it and believe I addressed the issues with exclusions not working as intended. The issues were twofold:

  1. The generic device/entity/label selector was not in sync with the report code. User could select to exclude objects that were not passed along to the exclusion statements.
  2. The exclusion selector only showed labels that were tied to devices with battery or similar diagnostic entities.
  3. User friendly name was not always shown in the unavailable device report.

Fork: https://gist.github.com/avogadro23/23f2e772d63b15a05239e73939623521

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment