Just a heads up and a question: These two sensors I’ve created to trigger alert automation had messed up my setup. Lights and switches, for example, took 3x the time to change state. I really need to create automation that can alert me if any device is unavailable or the battery_level attribute of my sensors are below 10.
Also, CPU usage and temperature became really high, lagging everything else.
Here are my custom sensors I’ve created:
- platform: template
sensors:
bateria_fraca:
friendly_name: 'Bateria fraca'
value_template: "{% for state in states if state.domain in ['switch','binary_sensor','sensor'] and state.attributes.battery_level is defined and state.attributes.battery_level < 10 -%}{%- if loop.first %}{% elif loop.last %} e {% else %}, {% endif -%}{{ state.name }} ({{ state.attributes.battery_level |int }}%){%- else -%}Nenhum{%- endfor %}"
icon_template: >
{% if is_state('sensor.bateria_fraca' , 'Nenhum') %}
mdi:check-circle
{% else %}
mdi:battery-alert
{% endif %}
dispositivo_indisponivel:
friendly_name: 'Indisponível'
value_template: "{% for state in states if ( state.domain in ['light','switch','binary_sensor','sensor'] and state.state == 'unavailable' ) -%}{%- if loop.first %}{% elif loop.last %} e {% else %}, {% endif -%}{{ state.name}}{%- else -%}Nenhum{%- endfor %}"
icon_template: >
{% if is_state('sensor.dispositivo_indisponivel' , 'Nenhum') %}
mdi:check-circle
{% else %}
mdi:alert-circle
{% endif %}
These sensors worked just fine, they show me the device that meet this criteria so I can just create an automation that triggers the state change of this template sensor. The idea sounds perfect for me…
Can anyone help me with this? To create an automation that alerts me using persistent notification, without me having to set manually all devices IDs or creating a sensor for each device…
I believe a timed automation searching for this criteria would be a good idea but I have not be able to create it.
Hello @NotoriousBDG! Thanks for your time to answer my question.
Unfortunatly I was unable to figure out how to adapt your script to my setup.
This script shows exactly all my devices with battery attributes lower than 10%, if not show “Nenhum”. This could work also with my other need, so I can find all devices with “Unavailable” state:
{% for state in states if state.domain in ['switch','binary_sensor','sensor'] and state.attributes.battery_level is defined and state.attributes.battery_level < 10 -%}{%- if loop.first %}{% elif loop.last %} e {% else %}, {% endif -%}{{ state.name }} ({{ state.attributes.battery_level |int }}%){%- else -%}Nenhum{%- endfor %}
Can you help me adapting this as a condition for an automation so it results with true if any is found or false for “Nenhum”?
I cannot make this to work as if any entity is found with this “for state” just result with “true”,
- condition: template
value_template: >-
{%- set threshold = 10 -%}
{%- for item in states.binary_sensor or states.sensor 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 loop.first -%}
{{ true }}
{%- endif -%}
{%- endfor %}
- condition: template
value_template: >-
{%- for item in states.binary_sensor or states.sensor or states.light or states.switch if item.state | lower == "unavailable" -%}
{%- if loop.first -%}
{{ true }}
{%- endif -%}
{%- endfor -%}
- alias: Alerta - Bateria fraca
trigger:
- platform: time
minutes: '/60'
seconds: 00
condition:
condition: template
value_template: >
{% set threshold = 10 -%}
{%- for item in states.binary_sensor or states.sensor if item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold or item.state | lower == "unknown" -%}
{%- if loop.first -%}
{{ true }}
{%- endif -%}
{%- endfor %}
action:
- service: persistent_notification.create
data:
message: "Bateria muito fraca do dispositivo {% for state in states if state.domain in ['switch','binary_sensor','sensor'] and state.attributes.battery_level is defined and state.attributes.battery_level < 10 -%}{%- if loop.first %}{% elif loop.last %} e {% else %}, {% endif -%}{{ state.name }} ({{ state.attributes.battery_level |int }}%){%- else -%}Nenhum{%- endfor %}"
title: "Bateria fraca"
- service: notify.ios_iphone_de_antonio
data_template:
title: "Alerta casa"
message: "Bateria muito fraca do dispositivo {% for state in states if state.domain in ['switch','binary_sensor','sensor'] and state.attributes.battery_level is defined and state.attributes.battery_level < 10 -%}{%- if loop.first %}{% elif loop.last %} e {% else %}, {% endif -%}{{ state.name }} ({{ state.attributes.battery_level |int }}%){%- else -%}Nenhum{%- endfor %}"
- alias: Alerta - Dispositivo indisponível
trigger:
- platform: time
minutes: '/30'
seconds: 00
condition:
condition: template
value_template: >
{% for item in states.binary_sensor or states.sensor or states.light or states.switch if item.state | lower == "unavailable" -%}
{%- if loop.first -%}
{{ true }}
{%- endif -%}
{%- endfor %}
action:
- service: persistent_notification.create
data:
message: "Bateria muito fraca do dispositivo {% for state in states if state.domain in ['switch','binary_sensor','sensor'] and state.attributes.battery_level is defined and state.attributes.battery_level < 10 -%}{%- if loop.first %}{% elif loop.last %} e {% else %}, {% endif -%}{{ state.name }} ({{ state.attributes.battery_level |int }}%){%- else -%}Nenhum{%- endfor %}"
title: "Bateria fraca"
- service: notify.ios_iphone_de_antonio
data_template:
title: "Alerta casa"
message: "Bateria muito fraca do dispositivo {% for state in states if state.domain in ['switch','binary_sensor','sensor'] and state.attributes.battery_level is defined and state.attributes.battery_level < 10 -%}{%- if loop.first %}{% elif loop.last %} e {% else %}, {% endif -%}{{ state.name }} ({{ state.attributes.battery_level |int }}%){%- else -%}Nenhum{%- endfor %}"
But there is still a problem: I will receive a notification every 30 minutes for the same device if it gets unavailable or every hour if battery is low…
I addressed that in my package as well. I split the automations up so notifications are only sent twice daily based on a time trigger, and persistent notifications are updated every 15 mins.