The script is not working and giving following error:
[140475850729024] UndefinedError: 'str object' has no attribute 'friendly_name'
[140476026635072] TypeError: can only concatenate list (not "str") to list
I want the script to produce an output of name-value pair for each battery, I get the correct output in the template editor:
Thanks for the recommendations and made few tweaks to my script and here is the updated code:
low_battery:
alias: 'Script: Low Battery Alert'
mode: single
variables:
a: !secret pankaj_email
b: !secret pankaj_cell
battery_sensors:
- sensor.frontdoor_battery
- sensor.slidingdoorbackyard_battery
- sensor.prats01_battery
- sensor.prats02_battery
- sensor.sid01_battery
- sensor.sid02_battery
- sensor.frontdoorinside_battery
- sensor.ewelink_th01_battery
- sensor.motionsensorfrontporchhue_battery
- sensor.motionsensordrivewayhue_battery
- sensor.backdoor_battery
- sensor.garage_motion_hue_battery
output_message: |
{% set x = (states.battery_sensors | selectattr('entity_id', 'search', 'battery') | map(attribute='name') ) %}
{% set ns = namespace( targets=[] ) %}
{% for b in battery_sensors %}
{% set ns.targets = ns.targets + [state_attr(b, 'friendly_name'), states(b)] %}
{% endfor %}{{ ns.targets }}
sequence:
- service: notify.smtp_1_gmail
data:
message: Test
title: "Battery Level Report"
target:
- "{{ a }}"
- "{{ b }}"
So the good news is that template is parsing correct values, in the script trace I see all the friendly names of the batteries with their level (under changed variables section of the trace):
As you can see in the format above the output is coming as a list when I wanted it to be a value pair like (‘Battery Driveway Hue’, ‘91’).
The smtp “message” is expecting a text or a string object and I cannot pass “output_message” list. Is there a way to format this list as a string that can passed to smtp message block?
I do the same basic thing as this, except I adopted naming conventions to help me eliminate other battery entities, like my phones and tablets so I check for all battery entities and then eliminate those that don’t actually have “battery_level” in the name.
@CO_4X4 and @pedolsky I’m basically trying to do the same thing as you guys i.e. eliminate several other entities that have battery (phone, tablet, solar panels etc).
One problem with my code is that I’ll need to keep maintaining the list of named sensors which may not be practical on the long haul.
Please post your code as it sounds more efficient than mine.
Thanks for your comments and really liked few things:
Custom attribute is awesome and wish I had created one ( beta_testing: true) for all the sensors I created on the fly to test things. The housekeeping for my code would have been very different but it is never too late to start!
Liked all the ways template logic can be addressed, very interesting and learned a lot!
Here is the final solution I’m leaning towards and can use some help with it.
There are over 30+ entities that have battery information in my house but there are only 10 that I need to keep track and I really want to avoid any manual code maintenance. But that seems unavoidable so trying to minimize the manual maintenance.
Created a group that has all the 10 devices that I need to track, this is the only place to maintain the details of monitored batteries going forward.
Created an input helper where I can set the threshold to monitor (currently set at 25%) below which the automation will send a notificaton for low battery level.
In the automation the condition checks if the output of below template is greater than “0” and it is working well.
The only thing I need some help with is tweaking the above template to output a list of names (not counts) that I can use in the message of SMTP notification but I cannot seem to make the tweak work.