Battery alert automation stuck with syntax

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

Where did you find it?

I’d like to see the source of this spaghetti before I proceed any further.

Also, do you have battery-powered lights and switches?


EDIT

Try this version. I tested it and confirmed it works on my system.

- alias: 'Battery alert'
  trigger:
  - platform: time
    at: '10:00:00'
  condition:
  - condition: template
    value_template: >-
      {%- set threshold = 40 -%}
      {%- for item in states 
          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:
  - service: notify.ios_renga
    data_template:
      title: '{{ as_timestamp(now()) | timestamp_custom("%d-%m-%Y, %H:%M:%S",True) }}'
      message: >-
        {%- set threshold = 40 -%}
        The following devices have low battery levels:
        {%- for item in states -%}
          {% if item.attributes.battery_level is defined and 
                item.attributes.battery_level | int < threshold %}
             {{ item.name }} ({{ item.attributes.battery_level }}),
          {% elif "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 -%}
2 Likes

I found it here- Howto create battery alert without creating a template for every device. Made a little modifications with early version. Your variant works like a charm, thank you.

1 Like