Your screenshot showed that the attribute was set correctly. Can you confirm you’re running the latest code from github? Can you also search the attributes column on your states page (http://hassio.local:8123/dev-state) for the friendly name of sensor.my_iphone_battery_level to make sure you don’t have a duplicate sensor created by MQTT discovery?
Perfect. That’s one of the main reasons I added that feature.
I rarely use the Home Assistant UI, so I haven’t even looked at lovelace yet. I’ll set it up so I can see if I can figure out what’s happening there.
Thank you for your assistance with this. I am running the latest code and only one of the sensors shows up on the states page. I know I have said it before but I want to make sure, I am not using MQTT discovery. Everything else but…I am also using notify.notify instead of slack but the sensors show up in both notify and persistent notifications. Are you using the battery_alert_disabled: true in your setup?
I also found out the the input_number errors on lovelace are related to using safari or firefox. When I use Chrome there are no errors. The issue has already been reported.
Thanks for confirming. I need to review the code again to see if there is a bug somewhere. I need to see your screenshot showing the attributes again so I can try to repro the problem. It looks like it’s been deleted.
Yes, I use it to disable alerts for my phone battery, just like you’re attempting to do.
Good to hear that it’s not an issue with the package.
I’m a little late to the thread, but you can simply your macro by not even calling out the domains because your for loop that searches for battery_level will remove every domain that doesn’t have that attribute:
{%- for item in states if (item.attributes.battery_level is defined) %}
{{ item.name }} #Do your message stuff here.
{%- endfor -%}
I no longer loop through all domains looking for battery_level attributes in the current version, so that doesn’t really apply anymore. That’s a useful tip though. I definitely have a use case for it.
I think the device needs to have a state change for it to be recognized. I noticed that a little late in the game. When I get a minute, I am going to try and take the mdi:battery off of one of my sensors and see if I can’t generate a state change for it to be recognized.
@petro, I played around today with the template editor and your code. It hit on all the right entities. Any chance you could help me make it into something similar to what NotoriousBDG has done but simplified. I am just looking for some simple automation to notify me when a battery gets low. I only have like 5 batteries and they all came up with:
{%- for item in states if (item.attributes.battery_level is defined) %}
{{ item.name }}
{%- endfor -%}
Just not really sure where to go from there. Once started and understood, I could build on it.
automation:
- alias: battery notifier
trigger:
- platform: time
minutes: '/15'
seconds: 00
condition:
- condition: template
value_template: >
# this is the threshold, if the battery goes below 10, the alert will occur.
{% set battery_alert_threshold = 10 %}
# the next templateline is complicated. It selects
# the any objects that have the battery attribute. Then compares the battery
# attribute to the battery_alert_threshold. If the battery is less than the threshold
# it adds the item to a list. If the list is greater than or equal to 1 item,
# the template will return True, otherwise false.
{{ states | selectattr('attributes.battery_level', 'defined') | selectattr('attributes.battery_level','<', battery_alert_threshold ) | list | length >= 1 }}
action:
- service: notify.notify
data_template:
title: "Batteries low"
message: >
{% set low_batteries = states | selectattr('attributes.battery_level', 'defined') | selectattr('attributes.battery_level','<', battery_alert_threshold ) | map(attribute='name') | list | join(', ') %}
Low batteries in the following devices: {{ low_batteries }}
Looking at it now, it doesn’t seem much simpler. But it should send you messages.
Ok, I have been playing around with the template editor. If everything was correct with my battery_alert_disabled: true setup, shouldn’t this template report true?
{% macro battery_level() %}
{%- for item in states.sensor if (
(
is_state_attr('item.entity_id', 'battery_alert_disabled', 'true')
)
) -%}
{% endfor -%}
{% endmacro %}
{{ battery_level() | trim != ""}}
in @NotoriousBDG’s file, he’s using a input_boolean to decide weather or not to send the notification.
The is_state_attr() method looks inside the states and returns an entitys attribute specified in the call.
The first requirement is the entity_id, the second is the attribute, and the 3rd is the result of what you expect it to be.
You are feeding it a string ‘item.entity_id’ and a result string of ‘true’. I can guarentee you don’t have a entity id that is ‘item.entity_id’. If you are trying to grab the variable item (coming from the for loop), then you need to remove the quotes on that.
Second, you are evaluating a true/false, you need to evaluate against true/false, not a string of true or a string of false. So remove the quotes from that.
Third, the ‘battery_alert_disabled’ attribute will only be used if you customize the entity to have that attribute. Did you do that?
Lastly, why aren’t you just following @NotoriousBDG’s installation process and using his code in the link he supplied? Copy and Paste? It should work without you having to deal with all this…