Howto create battery alert without creating a template for every device

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 :slight_smile:

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?

Thank you

Ian

Yes, I think that should work. I used template editor…:

{## Imitate available variables: ##}
{% set value_json = { "value": "ok" } %}

{%- if value_json.value == "ok" -%}
 Full
{%- else -%}
 Low
{%- endif -%}

and it returned Full

So…

homeassistant:
  customize:
    sensor.sensor_with_battery_attibute_template:
      battery_template_string: >-
        {%- if value_json.value == "ok" -%}
          Full
        {%- else -%}
          Low
        {%- endif -%}

Thanks for your quick reply, I’ve been playing with this for a while, but I can’t get it to work :slight_smile:
Looking in the Template editor, I placed the following, which came back with “ok”:

{{state_attr('camera.blink_front_door_2',"battery")}}

In MQTT Explorer I can see:

{
  "entity_id": "camera.blink_front_door_2",
  "battery": "ok",
  "delete_battery_sensor": "homeassistant/sensor/blink_front_door_2_battery"
}

So my final code for configuration.yaml is:

homeassistant:
  packages: !include_dir_named packages
  customize:
    camera.blink_front_door_2:
      battery_template_string: >-
        {%- if value_json.value == :"ok" -%}
          Full
        {%- else -%}
          Low
        {%- endif -%}

However blink Front Battery is still showing 0 in Lovelace,

Any help or pointers you can give will be appreciated?

Ian

I spotted an error in my copy/paste… == :"ok" should be == "ok"

(I edited the post above)

homeassistant:
  packages: !include_dir_named packages
  customize:
    camera.blink_front_door_2:
      battery_template_string: >-
        {%- if value_json.value == "ok" -%}
          Full
        {%- else -%}
          Low
        {%- endif -%}

You should see battery_template_string as a new attribute of camera.blink_front_door_2 after restarting HA.

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 :slight_smile:

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"
}

resulting in:

{
  "value": "ok"
}

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.

After re-reading your posts, your battery entity that reports “ok” in HA is sensor.blink_front_battery?
Try that name instead…

homeassistant:
  packages: !include_dir_named packages
  customize:
    sensor.blink_front_battery:
      battery_template_string: >-
        {%- if value_json.value == "ok" -%}
          Full
        {%- else -%}
          Low
        {%- endif -%}

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:

homeassistant:
  packages: !include_dir_named packages
  customize:
    sensor.lumi_lumi_weather_shower_power:
      battery_template: "{{ value_json.value | int * 33.33 }}"

This results in:

and

image

So it would appear that the script is not looking at the amended bettery_template?

What does the original blink battery sensor look like in HA?
I want to try emulating it on my system.

[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.

There is also

image

I tried changing the “off” to “Full” with JSON

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…

{
  "name": "Living Room Battery",
  "state_topic": "homeassistant/sensor/living_room_battery/state",
  "value_template": "{%- if value_json.value = "ok" -%}
 Full
{%- else -%}
 Low
{%- endif -%}",
  "icon": "mdi:battery",
  "unique_id": "living_room_battery",
  "json_attributes_topic": "homeassistant/sensor/living_room_battery/attributes"
}

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 " ".

climate.living_room:
  battery: ok
  battery_template_string: "{{ value_json.value|regex_replace(find='ok', replace='Full', ignorecase=True)|regex_replace(find='low', replace='Low', ignorecase=True) }}"

If I change the state to ‘low’ it changes to 0.0 though.
Do you know what state blink reports when not ok?

After some more reading and troubleshooting I have it working now!
It appears the template needs to be single line…

climate.living_room:
  battery: ok
#  battery_template_string: "{{ value_json.value|regex_replace(find='ok', replace='Full', ignorecase=True)|regex_replace(find='low', replace='Low', ignorecase=True) }}"
  battery_template_string: "{%- if value_json.value == 'ok' -%} Full {%- else -%} Low {%- endif -%}"

For some reason the MQTT discovery does not like:

  battery_template_string: >-
    {%- if value_json.value == 'ok' -%}
      Full 
    {%- else -%} 
      Low 
    {%- endif -%}

In MQTT it looks like this…

{
  "name": "Living Room Battery",
  "state_topic": "homeassistant/sensor/living_room_battery/state",
  "value_template": "{%- if value_json.value == 'ok' -%}
   Full 
 {%- else -%} 
   Low 
 {%- endif -%}",
  "icon": "mdi:battery",
  "unique_id": "living_room_battery",
  "json_attributes_topic": "homeassistant/sensor/living_room_battery/attributes"
}

and generates an error in HA:

Unable to parse JSON living_room_battery: '{ "name": "Living Room Battery", "state_topic": "homeassistant/sensor/living_room_battery/state", "value_template": "{%- if value_json.value == 'ok' -%} Full {%- else -%} Low {%- endif -%}", "icon": "mdi:battery", "unique_id": "living_room_battery", "json_attributes_topic": "homeassistant/sensor/living_room_battery/attributes" }'

@NotoriousBDG If my findings are accurate, Install Instructions 13. in battery_alert.yaml should be amended…

Yep that appears to have worked, thank you very much for your time and persistence.

Ian

You’re most welcome! I learned a lot in this exercise…

You also helped me to learn a lot, thanks again.

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 :slight_smile:

customize:

    camera.blink_front_door_2:
      battery_template_string: >-
        {%- if value_json.value == 'ok' -%}
        Full
        {%- else -%}
        Low
        {%- endif -%}

Really? Does MQTT look like this then?

{
  "name": "Living Room Battery",
  "state_topic": "homeassistant/sensor/living_room_battery/state",
  "value_template": "{%- if value_json.value == 'ok' -%}
  Full
  {%- else -%} 
  Low
  {%- endif -%}",
  "icon": "mdi:battery",
  "unique_id": "living_room_battery",
  "json_attributes_topic": "homeassistant/sensor/living_room_battery/attributes"
}

Documentation item 13 does not have indents either…

##     homeassistant:
##       customize:
##         sensor.sensor_with_battery_attibute_template:
##           battery_template_string: >-
##             {%- if value_json.value < 2 -%}
##             Low
##             {%- else -%}
##             Full
##             {%- endif -%}