I have previously had this setup with a telegram bot to notify me but i have had to do a reinstall and lost the setup as it was not in the backup.
my telegram bot notifier is working ok but when i execute the automation it doesnt include any of the device names at all. it has the message but no device
blueprint:
name: Low battery level detection for all battery sensors
description:
Regularly test all sensors with 'battery' device-class for crossing
a certain battery level threshold and if so execute an action.
domain: automation
input:
threshold:
name: Battery warning level threshold
description:
Battery sensors below threshold are assumed to be low-battery (as
well as binary battery sensors with value 'on').
default: 20
selector:
number:
min: 5.0
max: 100.0
unit_of_measurement: "%"
mode: slider
step: 5.0
time:
name: Time to test at
description: Test is run at the configured time
default: "10:00:00"
selector:
time: {}
day:
name: Weekday to test on
description:
"Test is run at configured time either every day (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.
default:
exclude_entities: []
selector:
target:
entity:
device_class: battery
actions:
name: Actions
description:
Send a notification or perform some other Action. `{{names}}` is
replaced with the names of the sensors low on battery.
selector:
action: {}
source_url: https://gist.github.com/josephquigley/03f5de47827f798539cebbe179e15bc9#file-jq-home-assistant-low-battery-blueprint-v1-yaml
variables:
day: !input day
threshold: !input threshold
exclude: !input exclude
names: "{% set ns = namespace(devices = [], device_ids = [], exclude=[]) %}
{% for entity_id in exclude.exclude_entities %}
{% set is_entity_only = device_id(entity_id) == None %}
{% set id = (entity_id if is_entity_only else device_id(entity_id))|string %}
{% set ns.exclude = ns.exclude + [id] %}
{% endfor %}
{% for platform in [states.binary_sensor, states.sensor] %}
{% for item in platform %}
{% set entity_id = item.entity_id|string %}
{% if is_state_attr(entity_id, 'device_class', 'battery') and is_state_attr(entity_id, 'state_class', 'measurement') %}
{% if is_state(entity_id, 'on') or (is_number(states(entity_id)) and states(entity_id)|int < threshold) %}
{% set is_entity_only = device_id(entity_id) == None %}
{% set id = (entity_id if is_entity_only else device_id(entity_id))|string %}
{% if not id in ns.device_ids and not id in ns.exclude %}
{% set device_name = device_attr(id|string, 'name') if device_attr(id|string, 'name_by_user') == None else device_attr(id|string, 'name_by_user') %}
{% set entity_name = state_attr(entity_id, 'name') if state_attr(entity_id, 'friendly_name') == None else state_attr(entity_id, 'friendly_name') %}
{% set name = entity_name if is_entity_only else device_name %}
{% set battery_type = states(entity_id~'_type')|string %}
{% set ns.device_ids = ns.device_ids + [id] %}
{% set battery_level_entity = entity_id if is_entity_only else device_entities(id)|first %}
{% set ns.devices = ns.devices + [{'id': id, 'name': name|trim ~ '\nLevel: ' ~ states(entity_id)|int ~ '% \nType: ' ~ battery_type ~ '\n' , 'battery_level': states(entity_id)|int, 'is_entity': is_entity_only}] %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{{ ns.devices|sort(attribute='battery_level')|map(attribute='name')|join('\n') }}"
trigger:
- platform: time
at: !input time
condition:
- condition: template
value_template:
"{{ names != '' and (day | int == 0 or day | int == now().isoweekday())
}}"
action:
- choose: []
default: !input actions
mode: single
I’ve also added some formatting with line breaks to the ‘names’ variable. This helps me see the information more clearly.
That’s how the message appears on Telegram:
And the code for the automatization (in case you want to use the same format):
The entity name for the battery type must be exactly the same as the battery sensor’s, but followed by ‘_type’. ie: sensor.philipspir1_battery → sensor.philipspir1_battery_type
If there’s no declared battery type entity or it doesn’t meet the rule above… it will assign ‘unknown’ for the battery type.
Thanks for this, works perfectly. Just one small adjustment as I have a few sensors which have battery_level as their entity name instead of just battery:
{% set battery_type = states(entity_id|replace('_level','')~'_type')|string %}
Hi,
I have created an automation using this blueprint quite a while ago. I am now getting an error for the automation as one of the device to be notified no longer valid due to an intentional change on the device. Hence I am trying to update or delete the automation but I am not able to edit the automation in the GUI nor find the automation in yaml.
I have the following lines in my configuration.yaml.
automation manual: !include_dir_merge_list automations/
automation ui: !include automations.yaml
I went through automations.yaml and the yaml files in automations/ but the automation is not listed. Any idea where it is stored ?
Thanks,