Can I set that as an automation condition that the result of a template isn't empty?

I have this template triggered every day at 10 that checks if anything in the house has a battery level below 20 % and sends a mail with the “offending” devices. This is the template, which filters out my car and phone as well as some non-numerical states:

{%- set sensors = states.sensor 
  | selectattr('object_id', 'search', 'battery') 
  | rejectattr('state', 'in', ['idle', 'unavailable', 'unknown', 'good','discharging']) 
  | rejectattr('entity_id', 'search', 'vog') 
  | rejectattr('entity_id', 'search', 'starter_battery') 
  | list -%} 
{%- for s in sensors if s.state | int(0) < 40 %} 
{{s.name}}: {{ s.state }}% 
{%- endfor %} 

But the mail is sent no matter what. So is there a way I can use a similar template to stop the automation if there are no devices that has less than 20 % battery left? If I could run the same template with “is empty” that would work, but I don’t think that’s a condition.

Modify the actions section of your automation so thst it creates a variable containing a list of entities with low batteries.

If the variable’s list is not empty, it proceeds to notify you otherwise it ends without notification.

actions:
  - variables:
      low_battery: >
        {{ states.sensor
          | selectattr('object_id', 'search', 'battery')
          | rejectattr('state', 'in', ['idle', 'unavailable', 'unknown', 'good','discharging'])
          | rejectattr('entity_id', 'search', 'vog')
          | rejectattr('entity_id', 'search', 'starter_battery')
          | list }}
  - if:  "{{ low_battery | count > 0 }}"
    then:
      - action: notify.whatever
        data:
          message: >
            {%- for s in low_battery if s.state | int(0) < 40 %}
            {{s.name}}: {{ s.state }}%
            {%- endfor %} 
Summary

Thanks for answering! The notification is a mail notifiction I have set up, but it doesn’t want to work. The 40 % btw is because I want to test that it works, I’ll set it to 20 when it works. I just don’t have anything below 20 right now. Here is the action part the way it is with the mail set:

</s> <s>actions:</s> <s> - variables:</s> <s> low_battery: ></s> <s> {{ states.sensor</s> <s> | selectattr('object_id', 'search', 'battery')</s> <s> | rejectattr('state', 'in', ['idle', 'unavailable', 'unknown', 'good','discharging'])</s> <s> | rejectattr('entity_id', 'search', 'vog')</s> <s> | rejectattr('entity_id', 'search', 'starter_battery')</s> <s> | list }}</s> <s> - if: "{{ low_battery | count > 0 }}"</s> <s> then:</s> <s> - action: notify.automatiseringsepost</s> <s> data:</s> <s> message: ></s> <s> {%- for s in low_battery if s.state | int(0) < 40 %}</s> <s> {{s.name}}: {{ s.state }}%</s> <s> {%- endfor %} </s> <s>

But from that I get Unable to determine action @ data[0] What am I doing wrong here?

This is the notification mail:

```

notify:
- name: “Automatiseringsepost”
platform: smtp
server: “send.somewhere.com
port: 2525
timeout: 15
sender: “[email protected]
encryption: none
username: “[email protected]
password: “VERY SECRET”
recipient:
- “[email protected]
sender_name: “Z-Wave-Pi i huset”
```

Sorry, my mistake. Indentation error. Thanks for answering! But I do not get a mail with 40 %, I assume that is because it is not defined as low battery. Or am I mistaken? I need it to react to at least 20 % because from there and down to non-functional is a very short time for my Z-Wave lock, so I always replace at 20.

Try this improved version.

actions:
  - variables:
      low_battery: >
        {% set x = states.sensor
          | selectattr('object_id', 'search', 'battery')
          | rejectattr('state', 'in', ['idle', 'unavailable', 'unknown', 'good','discharging'])
          | rejectattr('entity_id', 'search', 'vog')
          | rejectattr('entity_id', 'search', 'starter_battery')
          | list %}
        {% set y = x | map(attribute='state') | map('int', 0) | list %}
        {{ zip(x|map(attribute='name'),y) | selectattr(1, 'le', 40) | list }}
  - if:  "{{ low_battery | count > 0 }}"
    then:
      - action: notify.automatiseringsepost
        data:
          message: >
            {%- for s in low_battery %}
            {{s[0]}}: {{s[1]}}%
            {%- endfor %}

The threshold is set to “less than or equal to 40”. You set the threshold here:

selectattr(1, 'le', 40)
               ^    ^
               |    |
Less than      |    |
or Equal to ----    ---- Threshold value 

So you should get notified only if there’s at least one device with a battery level less than or equal to 40. Otherwise, no notification is sent.


EDIT

Correction. Fixed the for-loop.

Thank you very much! That works! Only a few details, is it possible to insert a line feed after each post, remove "Battery level: " and add a space before %? This is how it looks now.

I’d like this:

Røykvarsler WC oppe: 38 %
Utgansdør: 30 %
Stuebryter: 40 %
Dør nede: 40 %

Try this version. Notice the position of the hyphens.

{% for s in low_battery -%}
{{s[0]}}: {{s[1]}} %
{% endfor -%}
1 Like

Thank you so much! That works as I wanted to! :grin:

1 Like