Howto create battery alert without creating a template for every device

so your way, it just goes in the automations yaml?

I added you code to my automations and I have a bunch of these errors:

Invalid service data for group.set: Entity ID is an invalid entity id for dictionary value @ data['entities']. Got ''

Do you have any sensors setup that are set to the battery device class?

Ok, now we are getting somewhere. What does this do:

- wait_template: "{{ is_state('script.notify_ios_engine' , 'off') }}"
      timeout: '00:00:30'

    - service: script.notify_ios_engine
      data_template:
        message: *message
        who: "john"

and do you use these:

input_number:
  battery_alert_threshold_max:
    name: "Max Alert Threshold"
    icon: mdi:arrow-collapse-up
    mode: slider
    min: -1
    max: 100
    # initial: 40

  battery_alert_threshold_min:
    name: "Min Alert Threshold"
    icon: mdi:arrow-collapse-down
    mode: slider
    min: -1
    max: 100
    # initial: -1

Hi,

I am currently using the latest version of NotoriousBDG code. Works fine with the only exception: I cannot make battery_sensor_creation_disabled attribute to work. I have set this attribute to true in the customize section like this:

    device_tracker.oleg_bullhead:
      battery_sensor_creation_disabled: True

and I can see the attribute in the entities page like this:

    gps_accuracy: 29
    source_type: gps
    battery: 82
    tid: O
    battery_sensor_creation_disabled: true

But the sensor is still created (this is from entity_registry.yaml):

    sensor._battery_4:
      config_entry_id:
      name:
      platform: mqtt
      unique_id: oleg_bullhead_battery

I tried to remove this entry from entity_registry.yaml but it re-appears after HA is restarted.

Any help is appreciated.

@pdobrien3, you can use 1.1.0 release. The issue you had with battery_alert_disabled has been fixed. To avoid MQTT, just disable or remove automation.battery_sensor_from_battery_level_attribute, automation.battery_sensor_from_battery_attribute, and automation.battery_sensor_from_battery_numeric_attribute. Notifications don’t rely on MQTT at all.

@ton2008, after you add battery_sensor_creation_disabled, you need to remove the retained MQTT topics for that sensor. You can identify the topic names from the mqtt_config_topic and mqtt_state_topic attributes on the battery sensors. Run the commands below on a machine that has mosquitto_pub installed then restart Home Assistant.

mosquitto_pub -h MQTT_IP -u MQTT_USERNAME -P 'MQTT_PASSWORD' -r -n -t 'MQTT_CONFIG_TOPIC'
mosquitto_pub -h MQTT_IP -u MQTT_USERNAME -P 'MQTT_PASSWORD' -r -n -t 'MQTT_STATE_TOPIC'
1 Like

Thanks a lot! It did help!

1 Like

@NotoriousBDG, Thank you, thank you, thank you. I am now successfully using your code.

I have a question though. In looking at my battery sensors, the ones named

sensor.my_entity_battery_level are automatically added the the battery alert group. I also noticed that the sensors I created, named sensor.my_entity_battery are only added when I go into customize and add a mdi:battery.

If I change all the sensors to sensor.my_entity_battery_level, will it automatically be added to the battery status group?

That’s a bug. I’ll try to update the code this weekend to fix that.

Unfortunately, it won’t with the current code due to the same bug mentioned above. With the 1.1.0 version, it either needs device_class of battery or an icon with battery in the name. I’ll make sure this works correctly in the next release.

Ha, I am still learning Home Assistant. I wouldn’t follow my lead. I did notice that all the entities with batteries have a battery_level attribute. Is this common outside of my setup?

That is the most common method, but there are numerous exceptions. The automations that rely on MQTT discovery have been designed to handle all of the attribute variations that have been identified so far.

maybe a weird question, but is the mqqt part needed if you don’t have a mqqt sensor wich uses batteries?

It replaces the need to manually create template sensors for entities that have battery attributes. It determines if an entity (not just MQTT ones) has battery level stored as an attribute, and it automatically creates a sensor based on that attribute.

ok, I have been testing everything with the alert threshold at 100%. I recently moved it to 55% and had to restart HA for some other changes. I noticed all the automations were off. Is this normal or did something get bumped during the restart? All the automations should be kept on correct?

thanks

I’m really late to this but love the way this has gone from a really simply idea of about 50 lines of code to a quite sophisticated component of nearly 700 lines of code :slight_smile:

Can I ask a dumb question?

What is the significance of the ‘*’ and ‘&’ prefixes, as in for example:

  action:
    - condition: template
      value_template: *low_battery_check

and

  action:
    - condition: template
      value_template: &low_battery_check >

and

  action:
    - condition: template
      value_template: *low_battery_check
    - service: notify.telegram
      data_template:
        title: "Low Battery Levels"
        message: *message

I believe one of them, I think the &, defines the template for use later within the code. Then the other, the *, just calls the same template to keep from having to paste the same template over and over. Just allows for cleaner yaml.

2 Likes

Release 1.1.0 is available now on GitHub. Deleting battery sensor should be quite a bit easier now with this release. There is a new input text box named “MQTT Topic to Clear” that will send the appropriate MQTT message to clear retained topics. You just need to grab the values of mqtt_state_topic and mqtt_config_topic attributes from the sensor you wish to remove and enter them into the input text box one at a time. Restart Home Assistant, and the sensor should be gone.

Changelog

Added

  • Added input text box named MQTT Topic to Clear, which can be used to clear MQTT topics with retained messages
  • Set initial_state to on for all automations to ensure they are enabled on startup

Changed

  • Consider entities with battery in their entity_id or friendly_name a battery sensor if neither an icon nor a device class is defined
  • Consolidated the battery_sensor_from_* automations into a single automation

This is fixed in the 1.1.1 release.

I had a similar issue when I first started using Home Assistant. Some automations wouldn’t be enabled on startup. I never figured out the root cause, because I found a simple workaround using customize_domain that ensures all automations are enabled at startup.

To help, I’ve added initial_state: 'on' to the automations in the package so they should be enabled on start automatically now.

homeassistant:
  customize_domain:
    automation:
      initial_state: 'on'

Thanks for the kind words. It still surprises me too.

Those are a feature of yaml known as anchors and references. It allows you to eliminate some duplication of code. For example, *low_battery_check gets automatically replaced with the value after &low_battery_check. There is some examples of the feature at daemonl.

sorry for the dumb question, i have to create a templeta for every deivce that has a bettery sensor (for ex a xiaomi open close sensor)? or your code it will create for me? becouse i add your package but the only sensor i can see is from the xiaomi vacuum for wich i have manually create a sensor.

It should create the battery sensors automatically.

MQTT discovery is needed for that feature to work. Can you confirm that it’s enabled? Can you also post a screenshot of the entity that’s not working from the http://hassio.local:8123/dev-states page so I can see its entity_id and attributes?