Howto create battery alert without creating a template for every device

I found your offline code a nice idea, so i copy/pasted the code in a YAML-file and saved it in the same folder as where the YAML from @NotoriousBDG is. But than my HA would not start again; after moving the another folder (which does not get included in config), my HA starts again.

if its of any help: that’s what happens with my posted sensor template too…

ditched it and looking for @NotoriousBDG installment…

1 Like

You can check your config in settings, does this give any errors?

I have named my yaml: offline_alert.yaml

Like I said, I have very little experience in programming and I got it working for my config :smiley: I just used the template-option in HA to check the templates, and the config checker for any errors in the yaml code :slight_smile:

I always run that check, thanks. This time, it ran so long, i gave up waiting and decided to just try it, and if it wouldn’t work, just move the yaml out of the way. Which i eventually did. It’s o.k., maybe i give it another look in the future.

Hey,
I changed your automation for notification using slack to telegram as below, but I only get “Low Battery Levels” in the message, no values, any ideas?

- alias: battery_notification_telegram
  trigger:
    - platform: time
      at: '10:00:00'
    - platform: time
      at: '18:00:00'
    - platform: state
      entity_id:
        - input_number.battery_alert_threshold_min
        - input_number.battery_alert_threshold_max
  action:
    - condition: template
      value_template: *low_battery_check
    - service: notify.telegram
      data_template:
        message: "Low Battery Levels"
        data:
          attachments:
          - color: '#52c0f2'
            title: "These devices have low battery levels"
            text: *message

thanks:

  {%- set domains = ['light', 'switch', 'sensor', 'zwave', 'binary_sensor', 'camera'] -%}
  {%- for domain in domains -%}
    {%- for item in states[domain] 
      if 
          (item.attributes.battery_level is defined and 
           item.attributes['battery_level'] | int < states.input_number.battery_alert.state | int ) -%} 
         {{ item.name }} below {{states.input_number.battery_alert.state}}: {{item.attributes['battery_level']}} 
         {%- if not loop.last %} 
      {% endif -%}
    {%- endfor -%}

one step further: how to have it report: None below {{states.input_number.battery_alert.state}}, if none is below the threshold?
{%else%} None gives either as many results as there are items, or no result at all, depending on the place in the template…

I don’t use Telegram, so I can’t test this. Can you give this a shot?

- alias: battery_notification_telegram
  trigger:
    - platform: time
      at: '10:00:00'
    - platform: time
      at: '18:00:00'
    - platform: state
      entity_id:
        - input_number.battery_alert_threshold_min
        - input_number.battery_alert_threshold_max
  action:
    - condition: template
      value_template: *low_battery_check
    - service: notify.telegram
      data_template:
        title: "Low Battery Levels"
        message: *message

Your code seems to be incomplete so I’m not including it below. You can accomplish what you want like this.

{% macro battery_level() %}
  Your code here
{% endmacro %}

{% if battery_level() | trim != "" %}
{{ battery_level() }}
{% else %}
None
{% endif %}

thank you! this is my first macro… nice day it is, learned something new again. Have been looking for ways to create a ‘module’ like that before, but never new this is the way. Very nice, and so elegant.

btw, indeed missed a final {% endfor %} in the earlier snippet, must have been a cut and paste fluke. This now does the trick:

{% macro battery_level() %}
{%- set domains = ['light', 'switch', 'sensor', 'zwave', 'binary_sensor', 'camera'] -%}
  {%- for domain in domains -%}
    {%- for item in states[domain] 
      if 
          (item.attributes.battery_level is defined and 
           item.attributes['battery_level'] | int < states.input_number.low_lux.state | int ) -%} 
         {{ item.name }} below {{states.input_number.low_lux.state}}: {{item.attributes['battery_level']}} 
         {%- if not loop.last %} 
      {% endif -%}
    {%- endfor -%}
 {%- endfor -%}
{% endmacro %}

{% if battery_level() | trim != "" %}
{{ battery_level() }}
{% else %}
None
{% endif %}

That works, thanks.

1 Like

If you’ve created your battery sensors manually and don’t want them to be managed by the package, disable the 3 automations that start with “Battery Sensor from”. To delete the sensors that were created, you’ll need to clear the retained mqtt topics or reinstall the Mosquito addon.

That being said, my goal was to eliminate the need to manually create any battery sensors. You may have a better experience if you remove the sensors you manually created and let the package manage them for you.

Regarding notifications, those are handled by the automations that start with “Battery Notification”.

Check out the latest code on GitHub. It already has each battery on a new line instead of using commas.

1 Like

What do you mean by before MQTT?

Ah, gotcha. The battery_alert_disabled attribute doesn’t require any sensors to be created via MQTT. Actually, battery_notification, battery_notification_clear, battery_notification_slack, and update_battery_status_group_members automations don’t have a dependency on MQTT.

You can use the package and disable battery_sensor_from_battery_level_attribute, battery_sensor_from_battery_attribute, and battery_sensor_from_battery_numeric_attribute automations to prevent it from creating any sensors with MQTT.

Nope. That works independently from MQTT.

It should be in your main customize section from configuration.yaml, not in the package.

Can you search for sensor.my_iphone_battery_level on your states page (http://hassio.local:8123/dev-state) and paste a screenshot here so I can see the attributes?

I went through setup using the latest code on github and have some early errors with a config check. I’m using a fairly fresh 0.74.1 install.

Configuration invalid

Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/packages/battery_alert.yaml", line 174, column 1
expected <block end>, but found '}'
  in "/config/packages/battery_alert.yaml", line 499, column 1

I tried commenting out the homeassistant: line, but got additional errors on the first group:.

Am I missing something here?

Thanks

@pdobrien3, your screenshot shows sensor.dans_ipad_battery_level, but you mentioned having issues with sensor.my_iphone_battery_level. Is sensor.dans_ipad_battery_level the one that’s giving you trouble?

I wonder if there is a problem with the packages line in your configuration.yaml. Did you use packages: !include_dir_named packages or something else?

I have that line in the homeassistant: section at the top.

homeassistant:
  packages: !include_dir_named packages

Could these includes at the end be throwing things off?

### INCLUDES ###
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

Thanks

EDIT: Line 499 was from a line return due to the copy/paste! I fixed that and four or five others and it loaded up just fine. This has some great new features since I last worked with it. Thanks for all the effort you’ve put into this!

Thanks!

1 Like

@NotoriousBDG. Ok, I have been playing a little bit this am and made some progress.

I changed my configuration.yaml to read packages: !include_dir_named packages and that seem to make Home Assistant restart quicker as well as reduce the amount of errors I was getting. A couple things:

  1. battery_alert_disabled: true is still not working for sensor.my_iphone_battery_level sensors. I tried putting it before the friendly_name: in customize and that had no effect.

  2. sensor.first_floor_siren_battery will not be recognized by the automations. I solved this by using customize to give the entity a battery icon and it is now showing up.

  3. input_number.battery_alert_threshold_max and input_number.battery_alert_threshold_min cause issues with the lovelace ui. Throws a bunch of error messages. Probably and issue with lovelace but I wanted to bring it to your attention as an FYI.