Howto create battery alert without creating a template for every device

The entity_id of those battery sensors don’t have the entity_id that I expect to see. They should be sensor.fib_smoke_sensor1_battery, sensor.fib_smoke_sensor2_battery, sensor.fib_smoke_sensor3_battery, etc. I only have 1 zwave device on a z-stick, and it’s not exhibiting this behavior. I’m suspecting that has something to do with the object_id being different than what I see on my system.

@Tomahawk, can you run this automation on your system then trigger a state change on your smoke sensors? This automation should create a persistent notification for each smoke sensor showing the entity_id and the object_id from the event. If my theory is correct, you’ll see an object_id of brannalarm for all 3 of them.

- alias: object_id_notification
  trigger:
    - platform: event
      event_type: state_changed
  condition:
    - condition: template
      value_template: "{{ trigger.event.data is not none }}"
    - condition: template
      value_template: "{{ trigger.event.data.new_state is not none }}"
    - condition: template
      value_template: "{{ trigger.event.data.new_state.attributes is not none }}"
    - condition: template
      value_template: "{{ trigger.event.data.old_state is not none }}"
    - condition: template
      value_template: "{{ 'fib_smoke_sensor' in trigger.event.data.entity_id }}"
  action:
    - service: persistent_notification.create
      data_template:
        title: "{{ trigger.event.data.entity_id }}"
        notification_id: "{{ trigger.event.data.entity_id }}"
        message: >
          entity_id = {{ trigger.event.data.entity_id }} 
          object_id = {{ trigger.event.data.new_state.object_id }}

If you’re referring to the jinja template in step 10 of the install instructions, you paste that in http://hassio.local:8123/dev-template. The right hand side will show yaml code that you can use to create a group (Group - Home Assistant). The group yaml goes in your Home Assistant config and not in the package.

Ok i did it, but i added the group to the package and not to the groups.yaml… is this an error?

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.