This is a great idea!
I took the last change from @NotoriousBDG and converted it into a package that can pretty much just be dropped in and will work. Just update the notification to your preference.
This is a great idea!
I took the last change from @NotoriousBDG and converted it into a package that can pretty much just be dropped in and will work. Just update the notification to your preference.
Awesome @sjabby! You beat me to it. That’s what I was planning to attempt next . I’ve never created a package before, so this saved me a lot of time.
Cleaned up even more with help from @skalavala !
Removed the need for input boolean and separate sensor. Put the template code within the automation itself.
################################################################
## Packages / Battery levels
################################################################
################################################
## Customize
################################################
homeassistant:
customize:
################################################
## Node Anchors
################################################
package.node_anchors:
customize: &customize
package: 'battery_alert'
expose: &expose
<<: *customize
haaska_hidden: false
homebridge_hidden: false
################################################
## Group
################################################
group.battery_alert:
<<: *customize
friendly_name: "Battery Alert"
icon: mdi:steam
################################################
## Automation
################################################
automation.battery_alert:
<<: *customize
friendly_name: "Battery Alert"
################################################
## Group
################################################
group:
battery_alert:
control: hidden
entities:
- automation.battery_alert
################################################
## Automation
################################################
automation:
- alias: battery_alert
trigger:
- platform: time
at: '10:00:00'
- platform: time
at: '18:00:00'
condition:
- condition: template
value_template: >
{% macro battery_level() %}
{%- set threshold = 40 -%}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() |trim != "" }}
action:
- service: persistent_notification.create
data_template:
title: Low Battery levels
notification_id: low-battery-alert
message: >
{% macro battery_level() %}
{%- set threshold = 40 -%}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() }}
- service: notify.pushover
data_template:
title: "Battery status"
message: >
{% macro battery_level() %}
{%- set threshold = 40 -%}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level()}}
Very neat, great work, time to give it a test
@sjabby can you help me understand what the Node Anchors do? I have never seen that before and not sure what homebridge is.
Node Anchors is used to simplify your configurations and avoid repetitions of settings. So instead of repeting all settings for each entity you just add an anchor to the setting. Example:
Without anchors:
################################################
## Without Node Anchors and Merge Key Tags
################################################
homeassistant:
customize:
light.back_porch:
friendly_name: "Back Porch"
package: 'philips_hue'
light.chandelier:
friendly_name: "Chandelier"
package: 'philips_hue'
light.dining_room:
friendly_name: "Dining Room"
package: 'philips_hue'
light.entry_lamp:
friendly_name: "Entry Lamp"
package: 'philips_hue'
light.front_porch:
friendly_name: "Front Porch"
package: 'philips_hue'
The same config with anchors:
################################################
## With Node Anchors and Merge Key Tags
################################################
homeassistant:
customize:
package.node_anchors: # This is just a dummy entry
customize: &customize # Also a dummy entry that allows us to define the node anchor
package: 'philips_hue'
light.back_porch:
<<: *customize # This merges the keys/values from "&customize"
friendly_name: "Back Porch"
light.chandelier:
<<: *customize
friendly_name: "Chandelier"
light.dining_room:
<<: *customize
friendly_name: "Dining Room"
light.entry_lamp:
<<: *customize
friendly_name: "Entry Lamp"
light.front_porch:
<<: *customize
friendly_name: "Front Porch"
Full example from @dale3h: https://github.com/dale3h/homeassistant-config/blob/master/examples/yaml_anchors.yaml
Here’s an update to the package with an alert that automatically clears the persistent notification when the low batteries are fixed.
################################################################
## Packages / Battery levels
################################################################
################################################
## Customize
################################################
homeassistant:
customize:
################################################
## Node Anchors
################################################
package.node_anchors:
customize: &customize
package: 'battery_alert'
expose: &expose
<<: *customize
haaska_hidden: false
homebridge_hidden: false
################################################
## Group
################################################
group.battery_alert:
<<: *customize
friendly_name: "Battery Alert"
icon: mdi:steam
################################################
## Automation
################################################
automation.battery_alert:
<<: *customize
friendly_name: "Battery Alert"
automation.battery_alert_clear:
<<: *customize
friendly_name: "Battery Alert Clear"
################################################
## Group
################################################
group:
battery_alert:
control: hidden
entities:
- automation.battery_alert
- automation.battery_alert_clear
################################################
## Automation
################################################
automation:
- alias: battery_alert
trigger:
- platform: time
at: '10:00:00'
- platform: time
at: '18:00:00'
condition:
- condition: template
value_template: >
{% macro battery_level() %}
{%- set threshold = 40 -%}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() |trim != "" }}
action:
- service: persistent_notification.create
data_template:
title: "Low Battery levels"
notification_id: low-battery-alert
message: >
{% macro battery_level() %}
{%- set threshold = 40 -%}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() }}
- service: notify.slack_notify
data_template:
message: "Low Battery Levels"
data:
attachments:
- color: '#52c0f2'
title: "These devices have low battery levels"
text: >
{% macro battery_level() %}
{%- set threshold = 40 -%}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() }}
- alias: battery_alert_clear
trigger:
- platform: time
minutes: '/30'
seconds: 00
condition:
- condition: template
value_template: >
{% macro battery_level() %}
{%- set threshold = 40 -%}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() |trim == "" }}
action:
- service: persistent_notification.dismiss
data:
notification_id: low-battery-alert
This is great. Anyway to have a space separating the various entities that are pulled up by the automation in the persistant notification box?
There should already be a comma and space between each item like this Sensor 1 (32), Sensor 2 (30), Sensor 3 (Low)
. What does yours look like?
Here is the screenshot.
works perfectly! Thanks!
Brilliant work thanks to everyone involved. I need to lower the thresholds as I have Xiaomi sensors as well and most of them are shown as low
COuld i just “extend” this nice automation with device_tracker? Would be nice
@Maaniac, I think i found the issue. The notification was missing commas between domains. Can you try out this updated package below?
@thundergreen, Yes, it shouldn’t be difficult to add. If your device_tracker entity has an attribute named battery_level
, you’d only need to add device_tracker to the list of domains. Can you provide more details how your device_tracker entity tracks it’s battery?
################################################################
## Packages / Battery levels
################################################################
################################################
## Customize
################################################
homeassistant:
customize:
################################################
## Node Anchors
################################################
package.node_anchors:
customize: &customize
package: 'battery_alert'
expose: &expose
<<: *customize
haaska_hidden: false
homebridge_hidden: false
################################################
## Group
################################################
group.battery_alert:
<<: *customize
friendly_name: "Battery Alert"
icon: mdi:steam
################################################
## Automation
################################################
automation.battery_alert:
<<: *customize
friendly_name: "Battery Alert"
automation.battery_alert_clear:
<<: *customize
friendly_name: "Battery Alert Clear"
################################################
## Group
################################################
group:
battery_alert:
control: hidden
entities:
- automation.battery_alert
- automation.battery_alert_clear
################################################
## Automation
################################################
automation:
- alias: battery_alert
trigger:
- platform: time
at: '10:00:00'
- platform: time
at: '18:00:00'
condition:
- condition: template
value_template: >
{%- set threshold = 40 -%}
{% macro battery_level() %}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }}{% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() |trim != "" }}
action:
- service: persistent_notification.create
data_template:
title: "Low Battery levels"
notification_id: low-battery-alert
message: >
{%- set threshold = 40 -%}
{% macro battery_level(domain) %}
{%- for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{%- endif -%}
{%- if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {% endif %}{% endif -%}
{%- endfor -%}
{% endmacro %}
{%- set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] -%}
{%- for domain in domains if battery_level(domain) |trim != ""-%}
{{ battery_level(domain) }}{%- if not loop.last %}, {% endif -%}
{%- endfor -%}
- service: notify.slack_notify
data_template:
message: "Low Battery Levels"
data:
attachments:
- color: '#52c0f2'
title: "These devices have low battery levels"
text: >
{%- set threshold = 40 -%}
{% macro battery_level(domain) %}
{%- for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{%- endif -%}
{%- if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {% endif %}{% endif -%}
{%- endfor -%}
{% endmacro %}
{%- set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] -%}
{%- for domain in domains if battery_level(domain) |trim != ""-%}
{{ battery_level(domain) }}{%- if not loop.last %}, {% endif -%}
{%- endfor -%}
- alias: battery_alert_clear
trigger:
- platform: time
minutes: '/30'
seconds: 00
condition:
- condition: template
value_template: >
{%- set threshold = 40 -%}
{% macro battery_level() %}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }}{% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() |trim == "" }}
action:
- service: persistent_notification.dismiss
data:
notification_id: low-battery-alert
Is it possible to create sensors for all battery powered devices? I got a tip about this thread but i used to use Script to track all devices with a battery_level but in that thread the author also indicates that it will not be used when he created a PR because they said use templating. So my question would be how can one still do that? Example output that thread would create:
(in all fairness this template does a better job at finding all battery powered devices) would be sweet to have a similar look too. (its better to get a feeling on battery levels by looking at them every now and then to know when the time is getting close to replace them)
No, this won’t create the template sensors for you, but if you do create them, it should automatically alert on them if entity’s name contains battery.
One thing that could be done is to create a single template sensor similar to @tboyce1’s suggestion in Howto create battery alert without creating a template for every device, but make it include all entities that have betteries instead of just ones that are low. This wouldn’t be a clean as your screenshot, but it would give you a single place to look to see current battery status of everything.
I’ll give it a shot, i do like having both options. If i can manage, i’ll post it too of course. Have not done much with the templates
It’s probably possible with either an automation, custom component, or python script that runs every so often and creates/updates template sensors for any sensor that has a battery level attribute. I may play around with this when I have some time if nobody comes up with a better solution before then.
If the custom component from the other thread works for what you want, there’s really nothing wrong with just keeping it in your custom components for personal use. You could possibly modify it to pick up the same battery levels as the templates.