Howto create battery alert without creating a template for every device

There’s nothing wrong with that. You’ll just have to remember to recreate it whenever updating to a newer version of the package.

1 Like

Here is the result using your automation:

The jinja script gives the same inital result, - only this:

battery_status:
      name: Battery Status
      icon: mdi:battery-charging
      entities:
        - sensor.brannalarm_battery
        - sensor.toms_ipad_air_battery_level
        - sensor.toms_ipad_air_battery_state
        - sensor.toms_iphone_5_battery_level
        - sensor.toms_iphone_5_battery_state
        - sensor.varmtvanntank_vannsensor_batteryImitate available variables:

Still no new entires for sensor.fib_smoke_sensor1_battery, 2 and 3. Only “sensor.brannalarm_battery”.

Version 1.0.7 is now available at Home-AssistantConfig/packages/battery_alert.yaml at master · notoriousbdg/Home-AssistantConfig · GitHub.

Added

  • Added unique_id to disambiguate sensors with duplicate names
  • Added source info to mqtt config topic
  • Consider friendly names and entity_ids that end with _bat as a battery sensor (needed for rflink battery sensors)
  • Consider entities with icon of battery, battery-alert, or battery-unknown as a battery sensor regardles of its name
  • Consider entity_ids containing battery as a battery sensor

Changed

  • Reduce code duplication using yaml anchors and references
  • Changed MQTT topic to be based on entity_id
  • Improve method of filtering non-battery devices that have battery in their name. Detection is now based on icon rather than keywords.

@Tomahawk, this version should fix your issue. You’ll need to install the latest package, restart, force your smoke and flood sensors to update, then restart one more time. I’ve fixed the issue by setting the unique_id on the MQTT sensor to the object_id of the source sensor. Please let me know if this version fixes your issue.

@Wim_L, those sensors should be identified as batteries with this latest version. Please try and let me know how it goes.

2 Likes

@NotoriousBDG it’s working as expected now.

Ty! :smile:

1 Like

Finally got around to upgrading this, seems to be working well and my other sensors are not hidden, thanks @NotoriousBDG

1 Like

Works fine. I am getting the battery sensors as expected!

Just one thing that makes me scratch my head. When it comes to the technical naming conversions for the new battery level sensors. Witch as fare I understand is derived from the original sensors friendly name(?)
Ny%20sensor

For (my) case having the same friendly name on all smoke (brannalarm) sensors, the new battery level sensors are getting their name from friendly name and a number behind as the new sensors technical name. That makes me wonder, which new battery level sensor goes to which original sensor?

Maybe when creating the new battery level sensors, just use the original sensor technical name and add “_ZBAT”, “_BATLEV” or something….

1 Like

Apologies, I completely neglected to mention how to address that. MQTT seniors don’t offer a way to create different entity_id and friendly name in an automated way. The way I addressed the issue was to set the unique_id to the object_id of the source entity, which is more meaningful when you have duplicate friendly names.

To fix your issue, all you have to do is change the entity_id and/or name in entity_registry.yaml, then restart. The unique_id should make it obvious to see where they came from so you can give them better names.

1 Like

After upgrade and change to latest. Much better now. Currently also read out from current sensor without creating new sensor. Very good.

Thank you very much.

1 Like

Hi,

Please, someone can explainme how to use the file battery_alert.yaml?

I have many xiaomi sensors, and to know the status battery i have for each one a sensor… and file begins turn very big. I was reading the topic, where in the beginning, you use a input_boolean, then sensor and at the end and automation, like this:

Later @NotoriousBDG, takes a very good code, that maybe make easy the first option, but i dunno how put in HA.

https://github.com/notoriousbdg/Home-AssistantConfig/blob/master/packages/battery_alert.yaml

thanks

The latest code along with instructions are on the GitHub page. Is there a particular step that’s giving you trouble?

How to delete old mqtt cache data for battery. As I used the new battery_alert.yaml. This time it shows correct battery level, however it also show the old battery level (which is not correct look like old cache) from mqtt.

example show:
diana_owntrack_battery: 20 <== this is wrong and keep showing all the time.
diana_iphone_battery: 47 <== this is right

The jinja template doesn’t list entities/sensors with battery_level as an attribute, does the package read/include sensors with battery_level as an attribute in alerts?

jinja template:

 battery_status:
   name: Battery Status
   icon: mdi:battery-charging
   entities:
 {%- for item in states.sensor if (
   (
     not is_state_attr(item.entity_id, 'battery_alert_disabled', true)
   ) and (
     is_state_attr(item.entity_id, 'device_class', 'battery')
     ) or (
       is_state_attr(item.entity_id, 'icon', 'mdi:battery')
     ) or (
       is_state_attr(item.entity_id, 'icon', 'mdi:battery-alert')
     ) or (
       is_state_attr(item.entity_id, 'icon', 'mdi:battery-unknown')
     ) or (
       'battery' in item.entity_id | lower
       and item.attributes.icon is defined
       and 'battery' in item.attributes.icon | lower
     ) or (
       'battery' in item.name | lower
       and item.attributes.icon is defined
       and 'battery' in item.attributes.icon | lower
     ) or (
       (item.entity_id | lower).endswith('_bat')
     ) or (
       (item.name | lower).endswith('_bat')
     )
   ) %}
     - {{ item.entity_id }}
 {%- endfor -%}

result:

battery_status:
       name: Battery Status
       icon: mdi:battery-charging
       entities:
         - sensor.battery_status
         - sensor.vision_zp3111_multisensor_4in1_battery

sensor with attribute:

You can delete them by sending a null message to the mqtt topics for those sensors. For example:

mosquitto_pub -h YOURMQTTSERVER -p 1883 -u YOURMQTTUSER -P 'YOURMQTTPASSWORD' -t "homeassistant/sensor/diana_owntrack_battery/config" -r -n
mosquitto_pub -h YOURMQTTSERVER -p 1883 -u YOURMQTTUSER -P 'YOURMQTTPASSWORD' -t "homeassistant/sensor/diana_owntrack_battery/state" -r -n
mosquitto_pub -h YOURMQTTSERVER -p 1883 -u YOURMQTTUSER -P 'YOURMQTTPASSWORD' -t "homeassistant/sensor/diana_iphone_battery/config" -r -n
mosquitto_pub -h YOURMQTTSERVER -p 1883 -u YOURMQTTUSER -P 'YOURMQTTPASSWORD' -t "homeassistant/sensor/diana_iphone_battery/state" -r -n

Yes, that’s expected behavior. It does not show entities that have battery levels stored as attributes. There is an automation in the package that automatically creates stand-alone battery sensors for each entity that has battery_level attributes. The jinja code shows them instead of the ones with battery attributes.

Thank you.
image

1 Like

Will this work if I identified the 433 Sensors for my windows and doors as binary sensors?

THe mqtt message looks like you catch it
homeassistant/sensor/ge {“time” : “2018-05-21 16:59:59”, “model” : “Interlogix”, “id” : “xxxxxxx”, “device_type” : “contact”, “raw_message” : “611430”, “battery” : “OK”, “switch1” : “CLOSED”, “switch2” : “OPEN”, “switch3” : “CLOSED”, “switch4” : “OPEN”, “switch5” : “OPEN”}

I specified them as so:

  • platform: mqtt
    state_topic: “homeassistant/sensor/ge”
    name: “Back Door”
    payload_on: “OPEN”
    payload_off: “CLOSED”
    qos: 0
    device_class: door
    value_template: ‘{% if value_json.id is equalto “xxxxxx” %} {{value_json.switch1}} {% else %} {{states.sensor.back_door.state}} {% endif %}’

Yes, but you’ll need to create an mqtt sensor for the battery. Make sure it either has battery in the name or has device_class set to battery.

1 Like

Thank you very much. I will try this tonight.

Good evening.

After do the delete of old mqtt cache date ‘the retain mqtt message’ I got error with mqtt on all devices that use mqtt.
After restart ha many many time. mqtt come back and show unknown of battery from mqtt

mqtt_error

from mqtt process

1527253200: Received PINGREQ from homeassistant
1527253200: Sending PINGRESP to homeassistant
1527253205: Received PUBLISH from homeassistant (d0, q0, r0, m0, ‘owntracks/diana/dianaiphone/cmd’, … (41 bytes))
1527253205: Sending PUBLISH to homeassistant (d0, q0, r0, m0, ‘owntracks/diana/dianaiphone/cmd’, … (41 bytes))
1527253206: Received PUBLISH from homeassistant (d0, q0, r0, m0, ‘owntracks/me/suniphone/cmd’, … (41 bytes))
1527253206: Sending PUBLISH to homeassistant (d0, q0, r0, m0, ‘owntracks/me/suniphone/cmd’, … (41 bytes))

I may not have given you the right topic name. Can you use mosquitto_sub to check to see the right topic name and clear it? Another option, if you’re using hassio, is to uninstall/reinstall the mosquitto addon, which will wipe out all retained messages.

@NotoriousBDG

I tried this updated package but it seems “battery_sensor_from_battery_level_attribute” is triggering alot.
Is this suppose to happen?

Every 5-10 mins it triggers 10-15 times in a row…