Howto create battery alert without creating a template for every device

You’re missing the battery sensors. Since you don’t want to use mqtt, you have to create all of them manually.

Hi,

I have installed the package, but not all battery devices are created.
Some are created but not all
i get the error :

Log Details (ERROR)

Mon Sep 10 2018 10:47:00 GMT+0200 (West-Europa (zomertijd))
Error rendering data template: UndefinedError: ‘trigger’ is undefined

I need to see a screenshot of the entity_id and attributes for the sensors that aren’t working from the States page (http://hassio.local:8123/dev-state).

That can happen if you try to manually trigger automation.battery_sensor_from_attributes instead of letting the alert trigger naturally.

Hi @NotoriousBDG,

The package installed and it looks neat.
I have some xiaomi sensors, and they are not showing in the list (my iPhone does :grinning:). Is it because they are listed as binary sensors in Entities?

EDIT:
Just noticed the error messages on mqtt. Is it must? or its needed so the package will work? I dont have mqtt configured in HA at all…
I tried to configure mqtt as described (using the embeded server) but got the invalid syntax error, so I’ll wait for your respons before trying to mess with my python settings…

MQTT is required for it to automatically create battery sensors. If you already have discrete battery sensors, then you can skip it. Personally, I use Mosquitto as my MQTT broker, so I don’t have any experience with the embedded broker.

Thanks for the reply. I rather not deal with mqtt right now…

Can you give me a lead on how to create the discrete battery sensors manually, I will do it myself. I dont have that many sensors.

Thanks again.

Naor

1 Like

Got it! it workes… I gutted the entire code sections with refference to mqtt (otherwise I get a bunch of log mesages every few minutes).
Once i’ll get mqtt up and running i’ll redownload the latet package and relace it.

Thanks man, it looks really good!!

2 Likes

Here is a little template code I used to create the sensors. Simply paste it into the template editor; and then cut and paste the output into a battery.yaml in your sensor directory.

Then search/replace [[ to {{ and ]] to }}

You can also modify the code to build you a group as well easily.

It is much lighter on system resources to do it this way; albeit it is static and when you add devices you need to rerun the templates.

- platform: template
  sensors:
{%- for state in states.zwave if state.attributes.battery_level | trim != "" %}
    {{ state.entity_id.split('.')[1] }}_battery:
      friendly_name: '{{ state.entity_id.split('.')[1] }}_battery'
      unit_of_measurement: '%'
      device_class: battery
      value_template: >-
       [[ state_attr('{{ state.entity_id }}','battery_level') ]]
{% endfor -%}
4 Likes

Hey,

it took me a long time to get back to this but I was able to install your latest version (1.1.2 (2018-08-23)) to test the template feature for my devies that report battery in “volt”.

I looked at the examples in the documentation but I am not sure how to use them.
Should the customize part with the template be applied to the original entity (the source) or the created battery sensor??
And would a normal restart of HA be enough to get the template working or do I need to “delete” the battery sensor and clear the MQTT topic and have it be created again?

Yes

Deleting the sensor would be the fastest way, but it’s not necessary. If you don’t want to delete it, you’ll need to restart once after changing the customize section, then wait for or force a state change, then restart one more time.

Hi Notorious, thanks for the awesome work and time you’ve spent on this. Your work would merit being incorporated in the standard install package, or as a Hassio-addin.
One quick question: I have just installed a few Aquara modules that report temperature, humidity and barometric pressure. As there are 3 sensors created, there are also 3 battery sensors created per device, which all report the same level (no surprise there :wink:). Is that something that could be caught in the package, or something I would handle on my side, and if so, how?
Answer may be in the tread already, but it has become too long to scroll :slight_smile:

Just add battery_sensor_creation_disabled attribute to the redundant entities like below. You’ll need to clear the retained topics for the redundant battery sensors that were already created.

homeassistant:
  customize:
    sensor.sensor_with_battery_attribute:
      battery_sensor_creation_disabled: true
1 Like

Hi,

I am not sure what I may have missed but I dont seem to be able to get the template working to convert my Volts values into High and Low.
I will try to give you as much information here as possible. Let me know if you need anything else.
Using the script v. 1.1.2 (2018-08-23), I made no changes apart from the notification automations.

I have 4 radiator thermostats from Homematic which run on normal batteries and report that value in volts.
There is a screenshot of the original entities.
Then I used the customize file to give those original (source) entities the battery_template. Screenshot of that as well. I only did it to 2 out of the 4 (1 with the value beeing 2,4 the other one 2.4) (one with a dot and one with a comma).
Now those two thermostats dont show any battery sensor created from your script but I get lots of error messages in the log. Screenshot form the error messages as well. The error is the same for both entities so I dont know if the dor or comma in the value makes a difference…
The other thermostats are without the battery_template and they just show 2% as their battery sensor.

I should be High above 2,4V and Low below it. I found out (looking at my history) that below 2,4V reported form the thermostats it takes about 3-4 days for them to drain the battery and die… so that would be the ideal warning threshold.

Try this template:

{%- if value_json.value | int < 2.4 -%}
"Low"
{%- else -%}
"Full"
{%- endif -%}

I will try that and report back…
Is it correct that I replace the word “value” with “battery” in my case, since I want it to look at the attribute “battery” of the original entity ?

EDIT:
I changed the template to be the one you posted above, but I am still getting error messages in the log as before.
One thing I found that might help are the MQTT messages themself.
Here is the config message for one of the sensors:
homeassistant/sensor/oeq1709806_battery/config

{
“device_class”: “battery”,
“name”: “Küche Heizung Battery”,
“state_topic”: “homeassistant/sensor/oeq1709806_battery/state”,
“unit_of_measurement”: “%”,
“value_template”: “{%- if value_json.battery | int < 2.4 -%} “Low” {%- else -%} “Full” {%- endif -%}”,
“unique_id”: “oeq1709806_battery”,
“json_attributes”: [
“entity_id”,
“attribute”,
“mqtt_config_topic”,
“mqtt_state_topic”
]
}

This looks all good but the following state message might reveal the problem:
homeassistant/sensor/oeq1709806_battery/state

{
“value”:2,
“entity_id”: “climate.oeq1709806”,
“attribute”: “battery”,
“mqtt_config_topic”: “homeassistant/sensor/oeq1709806_battery/config”,
“mqtt_state_topic”: “homeassistant/sensor/oeq1709806_battery/state”
}

The value seems to miss the number after the comma…

This is true no matter if I write the template with “{%- if value_json.battery | int < 2.4 -%}” or “{%- if value_json.value | int < 2.4 -%}” !!!

This is a copy from the original entitiy from the states screen (as you can see in my screenshots above):

Entity:
climate.oeq1709806

State:
auto

Attributes:
current_temperature: 20
min_temp: 4.5
max_temp: 30.5
temperature: 8
operation_mode: auto
operation_list: auto,manual,boost,comfort,lowering
rssi: -56
valve: 0
battery: 2.5
mode: Auto
id: OEQ1709806
interface: wireless
friendly_name: Küche Heizung
supported_features: 129
battery_template: {%- if value_json.battery | int < 2.4 -%} “Low” {%- else -%} “Full” {%- endif -%}

Why is it not showing the number after the comma and why is it a comma at all, cause the original entity uses a dot and the template is also using a dot.

Ahh, thanks for posting the MQTT payload. The issue is that the template has quotes in it, which is making the json invalid. Change your template to what I show below then restart, force a state change, then restart one more time.

{%- if value_json.value | int < 2.4 -%}
Low
{%- else -%}
Full
{%- endif -%}

Thanks for the tip!
This fixed the errors in the log.
Now i have 2 battery sensors showing up as “Low %”.
I think now the template resolves to low because of the wrong value in the state mqtt message.
If you look at my previous post you can see that the state mqtt message actually shows

“value”:2,

And 2, < 2.4 would be true…
but right now the actual battery level reported by the device is “2.6”

The issue with Low % is that the automation assumes it’s a % value. Replacing Low and High with numbers like 30 and 100 respectively will work around that.

The issue with value being 2 instead of 2.4 looks like a bug. I’ll have to attempt to repro that issue to find the root cause.

{%- if value_json.value | float < 2.4 -%}
30
{%- else -%}
100
{%- endif -%}

If I can help you in any way let me know.
I would be glad to help find the cause