Hello everyone.
I found automation, that has simple discharged battery algorithm. But i’m stuck in syntax, jinja template checker works well, but in automation there is no action done, so i do not recieve any messages. Here is the code:
alias: Battery alert
trigger:
- at: '10:00:00'
platform: time
condition:
- condition: template
value_template: '{%- set threshold = 40 -%} {%- for item in states.light 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: '{%- set threshold = 40 -%} {%- for item in states.switch 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: '{%- set threshold = 40 -%} {%- for item in 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: '{%- set threshold = 40 -%} {%- for item in states.binary_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 -%}'
action:
- data_template:
message: '{%- set threshold = 40 -%} {%- set domains = [states.light, states.switch,
states.sensor, states.binary_sensor ] -%} {%- for domain in domains -%} {%-
if loop.first -%} The following devices have low battery levels: {%- endif
-%} {%- for item in 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"] }}), {% 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 }}), {% endif -%} {%- endfor -%} {%- endfor
-%}'
title: '{{ as_timestamp(now()) | timestamp_custom("%d-%m-%Y, %H:%M:%S",True)
}}'
service: notify.ios_renga