I just modified packages/battery_alert.yaml to support custom battery_alert_threshold attributes per sensor as I wanted an alert for my front door lock below 65 (when it hits < ~55-60 “she’s dead Jim”) but not for everything else…
Change::
and states(entity_id) | int < states.input_number.battery_alert_threshold_max.state | int
and states(entity_id) | int > states.input_number.battery_alert_threshold_min.state | int
to:
and (
states(entity_id) | int < states.input_number.battery_alert_threshold_max.state | int
or states(entity_id) | int < state_attr(entity_id, 'battery_alert_threshold') | int
)
New code:
action:
- condition: template
value_template: &low_battery_check >
{% macro battery_level() %}
{% for entity_id in states.group.battery_status.attributes.entity_id if (
not (
is_state_attr(entity_id, 'battery_alert_disabled', true)
or is_state_attr(entity_id, 'restored', true)
)
and states(entity_id) is not none
and (
(
(
states(entity_id) is number
or states(entity_id) | length == states(entity_id)| int | string | length
or states(entity_id) | length == states(entity_id)| float | string | length
)
and (
states(entity_id) | int < states.input_number.battery_alert_threshold_max.state | int
or states(entity_id) | int < state_attr(entity_id, 'battery_alert_threshold') | int
)
and states(entity_id) | int > states.input_number.battery_alert_threshold_min.state | int
)
or states(entity_id) | lower == 'low'
or states(entity_id) | lower == 'unknown'
or states(entity_id) | lower == 'unavailable'
Then add a new attribute battery_alert_threshold: 60 (or whatever value is ‘low’ for that sensor) to your battery sensor.
node_id: 13
unit_of_measurement: '%'
friendly_name: Front Door Lock Battery
device_class: battery
battery_alert_threshold: 60
What a great package this is, thank you for everyone’s hard work. I’m been reading through this topic, but can’t resolve my problem, hopefully someone could point me the in the right direction.
I ahve everything working fine, except my Blink camera
In the Lovelace Battery Alert View Card, the Battery Status list shows it as “blink Front Battery 0”
In Developer tools states, there are three Entities for battery, but the only one showing “0” state is “sensor.blink_front_battery” however it does have a battery attribute showing “ok”. This is reflected in MQTT Explorer as:
{
"value": "ok"
}
So my query is how do I correct this to show a more meaningful status such as “ok” and therefor also hopefully fix the alerts.
I wondered if I need a template to convert the string to Low or High perhaps, as per the example in the file?
Thanks for your quick reply, I’ve been playing with this for a while, but I can’t get it to work
Looking in the Template editor, I placed the following, which came back with “ok”:
Thanks for your patience, as you can tell JSON is not my strong point, but learning all the time. I did paste your template in to the development console, but of course the “:” was not in the template and only in the JSON code
I now have the following at the bottom of my “camera.blink_front_door_2 attributes”, is this correct?
However now the Blink camera battery does not appear in the Lovelace list?
other details snipped
motion_detected: false
wifi_strength: -72
network_id: 156136
sync_module: Home
last_record: null
friendly_name: blink Front
entity_picture: /api/camera_proxy/camera.blink_front_door_2?token=04c9e303ac96a66439b935ff31ac5e815a502c0324c146bdd8673f8802667b38
supported_features: 0
battery_template_string: {%- if value_json.value == "ok" -%}
Full
{%- else -%}
Low
{%- endif -%}
PS and MQTT Config message is:
{
"name": "blink Front Battery",
"state_topic": "homeassistant/sensor/blink_front_door_2_battery/state",
"value_template": "{%- if value_json.value == "ok" -%}
Full
{%- else -%}
Low
{%- endif -%}",
"icon": "mdi:battery",
"unique_id": "blink_front_door_2_battery",
"json_attributes_topic": "homeassistant/sensor/blink_front_door_2_battery/attributes"
}
I’ve never had to try doing this so…
After re-reading, re-re-reading and re-re-re-reading this section…
## 13. If a battery attribute requires a template to convert it into a string, use customize to add
## `battery_template_string` with the necessary template. The template result must be a string.
## For example, "Low" will trigger low battery notification).
##
## This example will create a battery sensor that contains "Low" if battery_level
## is less than 2
##
## homeassistant:
## customize:
## sensor.sensor_with_battery_attibute_template:
## battery_template_string: >-
## {%- if value_json.value < 2 -%}
## Low
## {%- else -%}
## Full
## {%- endif -%}
################################################################
I’m at a loss. I’ll have to do some dissection of the code for a better understanding.
Thank you.
No it looks like “sensor.blink_front_battery” is what is generated by the battery package.
There is a “binary_sensor.blink_front_battery” which generates “off” but as I had not seen that in MQTT explorer I did not think this was the correct one. But I have tried it with no luck.
To widen my fault finding I found a Xiaomi sensor that reports 28% from 2.8v, so I have tried this:
[quote=“dbrunt, post:909, topic:30576, full:true”]
What does the original blink battery sensor look like in HA?
I want to try emulating it on my system.
I’ve done some testing by adding attribute battery: ok to my climate.living_room. When climate.living_room state changes, the automation publishes to MQTT for HA sensor discovery. Without the battery_template_string: HA creates sensor.living_room_batttery with state “ok”. If I add battery_template_string to the equation, HA fails to create the sensor so I think HA is not liking something about the {%- … -%} code. There’s probably debug settings to see what HA is not liking but I’m not sure what those would be…
battery_template_string: >-
{%- if value_json.value == "ok" -%}
Full
{%- else -%}
Low
{%- endif -%}
You could for now just create your own HA templated sensor for the battery attribute from camera.blink_front_door_2 with device_type: battery. The package is supposed to automate that process for you but HA has to be barfing on the value_template…
It looks like it is getting more complicated than that on my side, mine creates sensor.blink_front_battery with a value of “0”
I am really grateful for your help so far, but I am very conscious that not only am I taking up lots of your time, I’m also clogging up this topic with my issue.
I’m think it would be prudent to wait and see if someone with a Blink camera can have a go at debugging, as I do not have the skills to do so?
The Blink app informes me when the battery is low, so as much as it would be nice to have it in HA, it is not an essential.
I have this working now when battery: ok.
One issue was “ok” needed to be ‘ok’ since the outer is wrapped in " " and the json string in MQTT is also wrapped in " ".
Out of interest, I have been experimenting, this code also works, they issue was the additional spaces that were in front of Full and Low. Good old YAML spacing
customize:
camera.blink_front_door_2:
battery_template_string: >-
{%- if value_json.value == 'ok' -%}
Full
{%- else -%}
Low
{%- endif -%}