Hello, i have downloaded this Package for receive notification of low battery in my devices:
################################################################
Battery Alert Package by NotoriousBDG
Source: https://raw.githubusercontent.com/notoriousbdg/Home-AssistantConfig/master/packages/battery_alert.yaml
################################################################
################################################################
## Install Instructions
################################################################
##
## 1. Enable MQTT using your preferred MQTT broker
## https://home-assistant.io/components/mqtt/
##
## 2. Enable MQTT Discovery by adding `discovery: true` and
## `discovery_prefix: homeassistant` under the `mqtt:` section
## of your configuration.yaml
##
## mqtt:
## discovery: true
## discovery_prefix: homeassistant
##
## 3. Save this file as CONFIGDIR/packages/battery_alert.yaml
##
## 4. Add `packages: !include_dir_named packages` under the
## `homeassistant:` section of your configuration.yaml
##
## homeassistant:
## packages: !include_dir_named packages
##
## 5. Restart Home Assistant
##
## 6. Adjust Min Alert Threshold slider to the minimum battery
## level to alert on. Note: the input slider requires the
## recorder component to keep it's state through restarts.
## If recorder is not enabled, the threshold can be set by
## un-remarking `initial` and setting the preferred default.
##
## 7. Adjust Max Alert Threshold slider to the maximum battery
## level to alert on. Note: the input slider requires the
## recorder component to keep it's state through restarts.
## If recorder is not enabled, the threshold can be set by
## un-remarking `initial` and setting the preferred default.
##
## 8. Setup notifications by entering the notifier name in the `Notifier Name` input text box.
## E.g. `notify.slack`. If no value is entered, it defaults to `notify.notify`.
##
## 9. Set the notifier format by selecting either `Default` or `Slack. Note: most notifiers
## are compatible with the `Default` format.
##
## 10. To disable alerts for a specific entity, use customize to
## set `battery_alert_disabled` to `true`
##
## homeassistant:
## customize:
## sensor.sensor_name_to_ignore_battery:
## battery_alert_disabled: true
##
## 11. To disable creating a sensor from battery attributes for a specific entity, use customize to
## set `battery_sensor_creation_disabled` to `true`
##
## homeassistant:
## customize:
## sensor.sensor_with_battery_attibute:
## battery_sensor_creation_disabled: true
##
## 12. If a battery attribute requires a template to convert it into a usable percent, use customize
## to add `battery_template` with the necessary template. The template result must be a number
## that represents a percentage.
##
## This example will create a battery sensor with the value of battery_level * 2
##
## homeassistant:
## customize:
## sensor.sensor_with_battery_attibute_template:
## battery_template: "{{ value_json.value | int * 2 }}"
##
## 13. If a battery attribute requires a template to convert it into a string, use customize to add
## `battery_template_string` with the necessary template. The template result must be a string.
## For example, "Low" will trigger low battery notification).
##
## This example will create a battery sensor that contains "Low" if battery_level
## is less than 2
##
## homeassistant:
## customize:
## sensor.sensor_with_battery_attibute_template:
## battery_template_string: >-
## {%- if value_json.value < 2 -%}
## Low
## {%- else -%}
## Full
## {%- endif -%}
################################################################
################################################
## Customize
################################################
homeassistant:
customize:
################################################
## Node Anchors
################################################
package.node_anchors:
customize: &customize
package: 'battery_alert'
################################################
## Group
################################################
group.battery_view:
<<: *customize
friendly_name: "Batterie"
icon: mdi:battery-alert
group.battery_alert:
<<: *customize
friendly_name: "Batterie Scariche"
icon: mdi:steam
control: hidden
group.battery_status:
<<: *customize
friendly_name: "Stato Batterie"
icon: mdi:battery-charging
control: hidden
################################################
## Automation
################################################
automation.battery_persistent_notification:
<<: *customize
friendly_name: "Battery Persistent Notification"
icon: mdi:comment-alert-outline
automation.battery_persistent_notification_clear:
<<: *customize
friendly_name: "Battery Persistent Notification Clear"
icon: mdi:comment-remove-outline
automation.battery_notification_default:
<<: *customize
friendly_name: "Battery Notification Default Format"
icon: mdi:comment-alert-outline
automation.battery_notification_slack:
<<: *customize
friendly_name: "Battery Notification Slack Format"
icon: mdi:comment-alert-outline
automation.battery_sensor_from_attributes:
<<: *customize
friendly_name: "Create Battery Sensor from Attributes"
icon: mdi:battery-charging-wireless-outline
automation.update_battery_status_group_members:
<<: *customize
friendly_name: "Update Battery Status Group Members"
icon: mdi:group
automation.delete_battery_sensor:
<<: *customize
friendly_name: "Delete a Battery Sensor"
icon: mdi:server-remove
################################################
## Group
################################################
group:
battery_view:
view: yes
entities:
- group.battery_status
- group.battery_alert
battery_alert:
control: hidden
entities:
- input_boolean.low_batteries
- input_number.battery_alert_threshold_min
- input_number.battery_alert_threshold_max
- input_text.delete_battery_sensor
- input_text.notifier_name
- input_select.notifier_format
- automation.battery_persistent_notification
- automation.battery_persistent_notification_clear
- automation.battery_notification_default
- automation.battery_notification_slack
- automation.battery_sensor_from_attributes
- automation.update_battery_status_group_members
- automation.delete_battery_sensor
################################################
## Input Boolean
################################################
input_boolean:
low_batteries:
name: Low Batteries
icon: mdi:battery-alert
################################################
## Input Number
################################################
input_number:
battery_alert_threshold_max:
name: "Soglia Massima Avviso"
icon: mdi:arrow-collapse-up
mode: slider
min: -1
max: 100
# initial: 40
battery_alert_threshold_min:
name: "Soglia Minima Avviso"
icon: mdi:arrow-collapse-down
mode: slider
min: -1
max: 100
# initial: -1
################################################
## Input Select
################################################
input_select:
notifier_format:
name: Formato Nofiche
options:
- Default
- Slack
icon: mdi:comment-question-outline
################################################
## Input Text
################################################
input_text:
delete_battery_sensor:
name: Elimina Sensore Batteria
mode: text
initial: ''
notifier_name:
name: App Notifica
mode: text
################################################
## Automation
################################################
automation:
- alias: battery_persistent_notification
initial_state: 'on'
trigger:
- platform: time_pattern
hours: '/3'
- platform: state
entity_id:
- input_number.battery_alert_threshold_min
- input_number.battery_alert_threshold_max
action:
- condition: template
value_template: &low_battery_check >
{% macro battery_level() %}
{% for entity_id in states.group.battery_status.attributes.entity_id if (
not is_state_attr(entity_id, 'battery_alert_disabled', true)
and states(entity_id) is not none
and (
(
(
states(entity_id) is number
or states(entity_id) | length == states(entity_id)| int | string | length
or states(entity_id) | length == states(entity_id)| float | string | length
)
and states(entity_id) | int < states.input_number.battery_alert_threshold_max.state | int
and states(entity_id) | int > states.input_number.battery_alert_threshold_min.state | int
)
or states(entity_id) | lower == 'low'
or states(entity_id) | lower == 'unknown'
or states(entity_id) | lower == 'unavailable'
)
) -%}
{{ state_attr(entity_id, "friendly_name") }} ({{ states(entity_id) }})
{% endfor -%}
{% endmacro %}
{{ battery_level() | trim != "" }}
- service: input_boolean.turn_on
data:
entity_id: input_boolean.low_batteries
- service: persistent_notification.create
data_template:
title: "Batterie Scariche"
notification_id: low_battery_alert
message: &message >
{% macro battery_level() %}
{% for entity_id in states.group.battery_status.attributes.entity_id if (
not is_state_attr(entity_id, 'battery_alert_disabled', true)
and states(entity_id) is not none
and (
(
(
states(entity_id) is number
or states(entity_id) | length == states(entity_id)| int | string | length
or states(entity_id) | length == states(entity_id)| float | string | length
)
and states(entity_id) | int < states.input_number.battery_alert_threshold_max.state | int
and states(entity_id) | int > states.input_number.battery_alert_threshold_min.state | int
)
or states(entity_id) | lower == 'low'
or states(entity_id) | lower == 'unknown'
or states(entity_id) | lower == 'unavailable'
)
) -%}
{{ state_attr(entity_id, "friendly_name") }} ({{ states(entity_id) }})
{% endfor -%}
{% endmacro %}
{{ battery_level() }}
- alias: battery_persistent_notification_clear
initial_state: 'on'
trigger:
- platform: time_pattern
minutes: '/15'
seconds: 00
- platform: state
entity_id:
- input_number.battery_alert_threshold_min
- input_number.battery_alert_threshold_max
action:
- condition: template
value_template: &low_battery_clear >
{% macro battery_level() %}
{% for entity_id in states.group.battery_status.attributes.entity_id if (
not is_state_attr(entity_id, 'battery_alert_disabled', true)
and states(entity_id) is not none
and (
(
(
states(entity_id) is number
or states(entity_id) | length == states(entity_id)| int | string | length
or states(entity_id) | length == states(entity_id)| float | string | length
)
and states(entity_id) | int < states.input_number.battery_alert_threshold_max.state | int
and states(entity_id) | int > states.input_number.battery_alert_threshold_min.state | int
)
or states(entity_id) | lower == 'low'
or states(entity_id) | lower == 'unknown'
or states(entity_id) | lower == 'unavailable'
)
) -%}
{{ state_attr(entity_id, "friendly_name") }} ({{ states(entity_id) }})
{% endfor -%}
{% endmacro %}
{{ battery_level() | trim == "" }}
- service: input_boolean.turn_off
data:
entity_id: input_boolean.low_batteries
- service: persistent_notification.dismiss
data:
notification_id: low_battery_alert
- alias: battery_notification_default
initial_state: 'on'
trigger:
- platform: time
at: '10:00:00'
- platform: time
at: '18:00:00'
- platform: state
entity_id:
- input_number.battery_alert_threshold_min
- input_number.battery_alert_threshold_max
action:
- condition: template
value_template: *low_battery_check
- condition: template
value_template: "{{ states.input_select.notifier_format.state == 'Default' }}"
- service_template: >
{% if "notify." in states.input_text.notifier_name.state %}
{{ states.input_text.notifier_name.state }}
{% else %}
notify.notify
{% endif %}
data_template:
title: "Batterie Scariche"
message: *message
- alias: battery_notification_slack
initial_state: 'on'
trigger:
- platform: time
at: '10:00:00'
- platform: time
at: '18:00:00'
- platform: state
entity_id:
- input_number.battery_alert_threshold_min
- input_number.battery_alert_threshold_max
action:
- condition: template
value_template: *low_battery_check
- condition: template
value_template: "{{ states.input_select.notifier_format.state == 'Slack' }}"
- service_template: >
{% if states.input_text.notifier_name.state != "" %}
{{ states.input_text.notifier_name.state }}
{% else %}
notify.notify
{% endif %}
data_template:
message: "Batterie Scariche"
data:
attachments:
- color: '#52c0f2'
title: "Questi dispositivi hanno le batterie scariche:"
text: *message
- alias: battery_sensor_from_attributes
initial_state: 'off'
trigger:
- platform: event
event_type: state_changed
condition:
- condition: template
value_template: "{{ trigger is not none }}"
- condition: template
value_template: "{{ trigger.event.data is not none }}"
- condition: template
value_template: "{{ trigger.event.data.old_state is not none }}"
- condition: template
value_template: "{{ trigger.event.data.new_state is not none }}"
- condition: template
value_template: "{{ 'battery' not in trigger.event.data.new_state.name | lower }}"
- condition: template
value_template: "{{ not is_state_attr(trigger.event.data.entity_id, 'device_class', 'battery') }}"
- condition: template
value_template: "{{ not is_state_attr(trigger.event.data.entity_id, 'battery_sensor_creation_disabled', true) }}"
- condition: or
conditions:
- condition: and
conditions:
- condition: template
value_template: "{{ trigger.event.data.new_state.attributes.battery_level is defined }}"
- condition: template
value_template: "{{ trigger.event.data.new_state.attributes.battery_level is not none }}"
- condition: and
conditions:
- condition: template
value_template: "{{ trigger.event.data.new_state.attributes.battery is defined }}"
- condition: template
value_template: "{{ trigger.event.data.new_state.attributes.battery is not none }}"
- condition: and
conditions:
- condition: template
value_template: "{{ trigger.event.data.new_state.attributes['Battery numeric'] is defined }}"
- condition: template
value_template: "{{ trigger.event.data.new_state.attributes['Battery numeric'] is not none }}"
- condition: and
conditions:
- condition: template
value_template: "{{ trigger.event.data.new_state.attributes.battery_critical is defined }}"
- condition: template
value_template: "{{ trigger.event.data.new_state.attributes.battery_critical is not none }}"
action:
- service: mqtt.publish
data_template:
topic: "homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/config"
retain: true
payload: &config_payload >-
{
{% if trigger.event.data.new_state.attributes.battery_level is defined -%}
{%- set attribval = trigger.event.data.new_state.attributes.battery_level -%}
{%- set attribname = 'battery_level' -%}
{%- elif trigger.event.data.new_state.attributes.battery is defined -%}
{%- set attribval = trigger.event.data.new_state.attributes.battery -%}
{%- set attribname = 'battery' -%}
{%- elif trigger.event.data.new_state.attributes['Battery numeric'] is defined -%}
{%- set attribval = (trigger.event.data.new_state.attributes['Battery numeric'] | int + 1) * 10 -%}
{%- set attribname = 'Battery numeric' -%}
{% elif trigger.event.data.new_state.attributes.battery_critical is defined -%}
{%- set attribval = trigger.event.data.new_state.attributes.battery_critical -%}
{%- set attribname = 'battery_critical' -%}
{%- endif -%}
"name": "{{ trigger.event.data.new_state.name }} Battery",
"state_topic": "homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/state",
{% if trigger.event.data.new_state.attributes.battery_template is defined -%}
"value_template": "{{ trigger.event.data.new_state.attributes.battery_template }}",
"unit_of_measurement": "%",
"device_class": "battery",
{% elif trigger.event.data.new_state.attributes.battery_template_string is defined -%}
"value_template": "{{ trigger.event.data.new_state.attributes.battery_template_string }}",
"icon": "mdi:battery",
{% elif trigger.event.data.new_state.attributes.battery_critical is defined -%}
"value_template": "{{ "{{" }} 'low' if value_json.value else 'full' {{ "}}" }}",
"icon": "mdi:battery",
{% else -%}
"value_template": "{{ "{{" }} value_json.value | int {{ "}}" }}",
{% if attribval | int == attribval or attribval | float == attribval or attribval | length == attribval | float | string | length or attribval | length == attribval | int | string | length -%}
"unit_of_measurement": "%",
"device_class": "battery",
{%- elif attribval == "High" or attribval == "Full" -%}
"icon": "mdi:battery",
{%- elif attribval == "Medium" or attribval == "Med"-%}
"icon": "mdi:battery-50",
{%- elif attribval == "Low" -%}
"icon": "mdi:battery-alert",
{%- else -%}
"icon": "mdi:battery-unknown",
{%- endif %}
{% endif -%}
"unique_id": "{{ trigger.event.data.entity_id.split('.')[1] }}_battery",
"json_attributes_topic": "homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/attributes"
}
- service: mqtt.publish
data_template:
topic: "homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/state"
retain: true
payload: >-
{
{% if trigger.event.data.new_state.attributes.battery_level is defined -%}
{%- set attribval = trigger.event.data.new_state.attributes.battery_level -%}
{%- elif trigger.event.data.new_state.attributes.battery is defined -%}
{%- set attribval = trigger.event.data.new_state.attributes.battery -%}
{%- elif trigger.event.data.new_state.attributes['Battery numeric'] is defined -%}
{%- set attribval = (trigger.event.data.new_state.attributes['Battery numeric'] | int + 1) * 10 -%}
{%- elif trigger.event.data.new_state.attributes.battery_critical is defined -%}
{%- set attribval = trigger.event.data.new_state.attributes.battery_critical -%}
{%- endif -%}
"value": {% if attribval | int == attribval -%}
{{ attribval | int }}
{%- elif attribval | float == attribval -%}
{{ attribval | float }}
{%- elif attribval | length == attribval | float | string | length -%}
{{ attribval | float }}
{%- elif attribval | length == attribval | int | string | length -%}
{{ attribval | int }}
{%- else -%}
"{{ attribval }}"
{%- endif %}
}
- service: mqtt.publish
data_template:
topic: "homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery/attributes"
retain: true
payload: >-
{
{% if trigger.event.data.new_state.attributes.battery_level is defined -%}
{%- set attribval = trigger.event.data.new_state.attributes.battery_level -%}
{%- set attribname = 'battery_level' -%}
{%- elif trigger.event.data.new_state.attributes.battery is defined -%}
{%- set attribval = trigger.event.data.new_state.attributes.battery -%}
{%- set attribname = 'battery' -%}
{%- elif trigger.event.data.new_state.attributes['Battery numeric'] is defined -%}
{%- set attribval = (trigger.event.data.new_state.attributes['Battery numeric'] | int + 1) * 10 -%}
{%- set attribname = 'Battery numeric' -%}
{%- elif trigger.event.data.new_state.attributes.battery_critical is defined -%}
{%- set attribval = trigger.event.data.new_state.attributes.battery_critical -%}
{%- set attribname = 'battery_critical' -%}
{%- endif -%}
"entity_id": "{{ trigger.event.data.entity_id }}",
{% if attribname is defined -%}
"{{ attribname }}": "{{ attribval }}",
{%- endif %}
"delete_battery_sensor": "homeassistant/sensor/{{ trigger.event.data.entity_id.split('.')[1] }}_battery"
}
- alias: update_battery_status_group_members
initial_state: 'on'
trigger:
- platform: time_pattern
hours: '/6'
action:
- service: group.set
data_template:
object_id: "battery_status"
entities: >-
{%- for item in states.sensor if (
not is_state_attr(item.entity_id, 'hidden', true)
and (
is_state_attr(item.entity_id, 'device_class', 'battery')
or 'battery' in item.attributes.icon | lower
or (item.entity_id | lower).endswith('_bat')
or (item.name | lower).endswith('_bat')
) or (
(
'battery' in item.entity_id | lower
or 'battery' in item.name | lower
) and (
item.attributes.icon is not defined
) and (
not is_state_attr(item.entity_id, 'battery_alert_disabled', true)
)
)
)
-%}
{{ item.entity_id }}{% if not loop.last %}, {% endif %}
{%- endfor -%}
- alias: delete_battery_sensor
initial_state: 'on'
trigger:
- platform: state
entity_id:
- input_text.delete_battery_sensor
condition:
- condition: template
value_template: "{{ 'homeassistant/sensor/' in states.input_text.delete_battery_sensor.state }}"
action:
- service: mqtt.publish
data_template:
topic: "{{ states.input_text.delete_battery_sensor.state }}"
retain: true
- service: mqtt.publish
data_template:
topic: "{{ states.input_text.delete_battery_sensor.state }}/attributes"
retain: true
- service: mqtt.publish
data_template:
topic: "{{ states.input_text.delete_battery_sensor.state }}/state"
retain: true
- service: mqtt.publish
data_template:
topic: "{{ states.input_text.delete_battery_sensor.state }}/config"
retain: true
But i receive this error:
2020-01-04 04:45:00 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'None' has no attribute 'attributes'
2020-01-04 04:45:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
2020-01-04 05:00:00 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'None' has no attribute 'attributes'
2020-01-04 05:00:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
2020-01-04 05:15:00 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'None' has no attribute 'attributes'
2020-01-04 05:15:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
2020-01-04 05:30:00 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'None' has no attribute 'attributes'
2020-01-04 05:30:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
2020-01-04 05:45:00 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'None' has no attribute 'attributes'
2020-01-04 05:45:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
2020-01-04 06:00:00 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'None' has no attribute 'attributes'
2020-01-04 06:00:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification: Test condition template: False
2020-01-04 06:00:00 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'None' has no attribute 'attributes'
2020-01-04 06:00:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
2020-01-04 06:15:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
2020-01-04 06:30:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
2020-01-04 06:45:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
2020-01-04 07:00:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
2020-01-04 07:15:00 INFO (MainThread) [homeassistant.helpers.script] Script battery_persistent_notification_clear: Test condition template: False
and i think depends from this part of the package but i don’t know what is the problem:
- alias: battery_persistent_notification_clear
initial_state: 'on'
trigger:
- platform: time_pattern
minutes: '/15'
seconds: 00
- platform: state
entity_id:
- input_number.battery_alert_threshold_min
- input_number.battery_alert_threshold_max
action:
- condition: template
value_template: &low_battery_clear >
{% macro battery_level() %}
{% for entity_id in states.group.battery_status.attributes.entity_id if (
not is_state_attr(entity_id, 'battery_alert_disabled', true)
and states(entity_id) is not none
and (
(
(
states(entity_id) is number
or states(entity_id) | length == states(entity_id)| int | string | length
or states(entity_id) | length == states(entity_id)| float | string | length
)
and states(entity_id) | int < states.input_number.battery_alert_threshold_max.state | int
and states(entity_id) | int > states.input_number.battery_alert_threshold_min.state | int
)
or states(entity_id) | lower == 'low'
or states(entity_id) | lower == 'unknown'
or states(entity_id) | lower == 'unavailable'
)
) -%}
{{ state_attr(entity_id, "friendly_name") }} ({{ states(entity_id) }})
{% endfor -%}
{% endmacro %}
{{ battery_level() | trim == "" }}
- service: input_boolean.turn_off
data:
entity_id: input_boolean.low_batteries
- service: persistent_notification.dismiss
data:
notification_id: low_battery_alert
I wil wait some help!! thanks a lot
Stefano