Howto create battery alert without creating a template for every device

ok. thanks for the quick response. i’ve been having issues trying to load a package. in my configuration.yaml i have the following but it appears i’m getting an error loading the package:

homeassistant:
  packages:
     battery_alert: !include low_battery_package.yaml

then in the package file i have the following:

################################################################
## Packages / Battery levels 
################################################################

################################################
## Customize
################################################

homeassistant:
  customize:
  ################################################
  ## Node Anchors
 ################################################

    package.node_anchors:
      customize: &customize
        package: 'battery_alert'

      expose: &expose
        <<: *customize
        haaska_hidden: false
        homebridge_hidden: false

  ################################################
  ## Group
  ################################################
  
  group.battery_alert:
    <<: *customize
    friendly_name: "Battery Alert"
    icon: mdi:steam

  ################################################
  ## Sensor
  ################################################

  sensor.battery_status:
    <<: *customize
    friendly_name: "Battery Status"
    icon: mdi:battery

################################################
## Automation
################################################
#   automation.battery_status_update:
#     <<: *customize
#     friendly_name: "Battery Status Update"

#    automation.battery_alert:
#      <<: *customize
#      friendly_name: "Battery Alert"

################################################
## Input Boolean
################################################

input_boolean.battery_status_update:
  <<: *customize
  friendly_name: "Battery Status Update"
  icon: mdi:battery-charging-wireless


################################################
## Group
################################################

group:
  battery_alert:
    control: hidden
    entities:
      - input_boolean.battery_status_update
      - sensor.battery_status
#      - automation.battery_status_update
#      - automation.battery_alert

################################################
## Sensor
################################################
sensor:
  - platform: template
    sensors:
      battery_status:
        friendly_name: "Battery Status"
        entity_id:
          - input_boolean.battery_status_update
            value_template: >
              {%- set threshold = 40 -%}
              {%- set domains = ['binary_sensor', 'switch', 'sensor', 'zwave', 'lock'] -%}
              {%- for domain in domains -%}
              {%- for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and 
              ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
              {%- if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
              {{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
             {%- if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
             {{ item.name }} ({{ item.state }}){%- if not loop.last %}, {% endif -%} {% endif -%}
             {%- endfor -%}
             {%- endfor -%}

################################################
## Input Boolean
################################################

input_boolean:
  battery_status_update:
  initial: on

found the problem. i decided to just copy paste the package and go the method to include packages in a packages directory.

created a packages directory with the file battery_alert.yaml in it. and changes my config.yaml to

homeassistant:
  packages: !include_dir_named packages

and that seemed to load the package and clear the errors. now it looks like i just need to tweak the package to work the way i want for me. thx again

1 Like

Hi, mhm that’s strange because I am using the latest version from Github. [Release 1.1.6 (2018-11-21)]

I also restarted already several times and except the Homematic Sensors, which report battery states like “High” everything is fine.

Do you have any other idea, what I could do to get also this sensors working correctly?

Thanks

@NotoriousBDG I opened an issue on the github repo:

Try copying the value of mqtt_state_topic attribute and pasting into MQTT Topic to Clear" then do the same with mqtt_config_topic`. Confirm that the sensor is gone, then force a state change to recreate the sensor. If you still have issues, please send new screenshots of the source and battery sensor from the states page so I can see the state and attributes.

Thanks for the heads up. I’ll publish an update to switch away from the deprecated json_attibutes to json_attributes_topic once 0.85 is released.

1 Like

Still the same issue after clearing - screenshots of the devices

Thanks for the screenshots. I see now that there is an issue with the way attributes storing strings are handled. I need to update the code to handle that use case. I’ll be sure to include the fix it in the next release.

In the meantime, you should be able to workaround it by using templates. Once you change the template and restart, clear the MQTT topics again and force another state change.

Option 1: This option keeps the string values and will remove the %.

homeassistant:
  customize:
    climate.000393c99b950e:
      battery_template_string: >-
        {{ value_json.value }}

Option 2: This pption converts the strings to integers so the % makes more sense and the graph will work.

homeassistant:
  customize:
    climate.000393c99b950e:
      battery_template: >-
        {%- if value_json.value == "High" -%}
        100
        {%- elif value_json.value == "Medium" -%}
        50
        {%- elif value_json.value == "Low" -%}
        0
        {%- else -%}
        unknown
        {%- endif -%}
1 Like

I am still having an issue trying to get rid of an old battery entiity which no longer exists… The entity was renamed and now shows up as both the old and new, with the old entity listed as unavailable, causing an error in the log. what mqtt topic(s) need(s) to be removed to clear it away for good?

Those things can be like Zombies!
When it happens to me I delete the MQTT Integration, delete the mosquito addon (I don’t have any config mqtt entries now but if I did I would comment those out as well) then restart home assistant. Then edit the file /config/.storage/core.entity_registry and very carefully delete the zombies and make sure you don’t screw up the brackets etc.

Then restart Home Assistant and make sure they are still gone. Then add back mqtt in config if you use it, add the addon and add the integration and restart home assistant again… all going well everything will be back working with no zombies.

1 Like

hi, i have this problem:

group.battery_status unknown

but the sensor battery is ok
[sensor.alessandro_redmi_pro_battery] 23
device_class: battery
battery_level: 23
unit_of_measurement: %
friendly_name: Alessandro Redmi Pro Battery

[sensor.valentina_huawei_p10_lite_battery] 39
device_class: battery
battery_level: 39
unit_of_measurement: %
friendly_name: Valentina Huawei p10 lite Battery

Thank you very much - works now perfectly with the customization.

BR
Chris

1 Like

For that sensor, copy the value of the mqtt_state_topic attribute and paste it into MQTT Topic to Clear then do the same with mqtt_config_topic.

Can you clarify what problem you’re having? What you provided looks ok to me.

what would the topics be though? entity is sensor.bog_arum_battery_level

EDIT: strangely… I also have all the other old sensors in my entity register in the dev panel for that same item. It is a xiaomi plant sensor so it also has temp, moisture, conductivity and light level. Again, these were all changed to a new entity name as a whole and there is no longer any code anywhere that relates to ‘bog_arum’ so I don’t know how else to get rid of these. They all show as ‘unavailable’. I’m guessing its all mqtt related?

The attributes I referenced have the topic names.

Dave - the only way I could get rid of the zombies was to follow the steps I posted earlier.

He posted the exact sensor he wants to get rid of… but your posts are too vague to be of any help in deleting the sensor… When I had the same problem someone posted something more specific but I still couldn’t get rid of the zombies. From what you posted I have even less idea what someone should actually practicall do to delete them.

I don’t even understand what you even mean by that. Paste it into where exactly? I did try posting a null string to the MQTT topic for state and cmnd but that didn’t help me either… In the end I did what I posted earlier.

Just updated to 0.85, now getting these errors at startup, which i believe are due to the battery package…

2019-01-10 16:30:50 WARNING (MainThread) [homeassistant.components.sensor.mqtt] configuration variable "json_attributes" is deprecated, replace with "json_attributes_topic"

and

It’s ok status unknown?

group.battery_status unknown

Hey Dave, if you’re using the plant: monitor, be sure to hash this out in your config. I had to do this with mine.

there is no mqtt topic for these entities…

yeah, the plant monitor was also renamed to the new entity so I literally have zero reference to this ‘bog_arum’ plant anywhere in my code. I dont know why HA thinks it needs to look for it still…