Howto create battery alert without creating a template for every device

I was doing from HA and not from the command line, now I did from both and they were still there.
Now come (again) another funny thing, only thing I could think of was a cache or something so I cleared cache on my browser and restart HA again and 3 of them vanished (still don’t understand why!).
But there’s still one (Pressure Downstairs) there and I can’t get rid of it even going through different browsers.
mosquitto_sub shows topics are empty.

It’s finally done, @NotoriousBDG!
Man heh, it was really driving me nuts.
What really helped was doing the following:

mosquitto_sub -t “homeassistant/sensor/#” | grep -i pressure

I could still see the pressure topics there, I sent a null, cleared the cache on the browser, restarted HA and voila, everything is as it should be.
Again, I’d like to thank you for the help here and for the entire thing, it’s really helpful!

2 Likes

Glad to hear you got it working. Happy to help.

1 Like

Hello, just wanted to let you (and possibly others) know that I have used your code and adapted it to show offline devices! But now I see that I used an old version :smiley:
I know very little about programming, so I just tried a few changes and got it working. Maybe (probably) there are errors in there, but it appears to be working :slight_smile:

################################################################
## Packages / Offline devices alert
################################################################

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

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

package.node_anchors:
  customize: &customize
    package: 'offline_alert'

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

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

group.offline_alert:
  <<: *customize
  friendly_name: "Offline devices Alert"
  icon: mdi:steam

################################################
## Automation
################################################

automation.offline_alert:
  <<: *customize
  friendly_name: "Offline devices Alert"

automation.offline_alert_clear:
  <<: *customize
  friendly_name: "Offline devices alert Clear"

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

group:
  offline_alert:
control: hidden
entities:
  - automation.offline_alert
  - automation.offline_alert_clear

################################################
## Automation
################################################

automation:
- alias: offline_alert
  trigger:
- platform: time
  at: '10:00:00'
- platform: time
  at: '18:00:00'
  condition:
- condition: template
  value_template: >
      {% macro offline_state() %}
      {%- set threshold = 40 -%}
      {% set domains = ['light', 'switch', 'sensor', 'binary_sensor', 'zwave', 'lock'] %}
      {% for domain in domains -%}
      {% for item in states[domain] if ((item.state | lower == "unavailable")) -%}
      {{ item.domain }} {{ item.name }} {%- if not loop.last %}, {% endif -%}
      {% endfor %}
      {%- endfor %}
      {% endmacro %}
      {{ offline_state() |trim != "" }}
  action:
- service: persistent_notification.create
  data_template:
    title: "Offline devices"
    notification_id: offline-devices-alert
    message: >
      {% macro offline_state() %}
      {% set domains = ['light', 'switch', 'sensor', 'binary_sensor', 'zwave', 'lock'] %}
      {% for domain in domains -%}
      {% for item in states[domain] if ((item.state | lower == "unavailable")) -%}
      {{ item.domain }} {{ item.name }} {%- if not loop.last %}, {% endif -%}
      {% endfor %}
      {%- endfor %}
      {% endmacro %}
      {{ offline_state() }}
- service: notify.slack_notify
  data_template:
    message: "Offline devices"
    data:
      attachments:
      - color: '#52c0f2'
        title: "These devices are offline"
        text: >
          {% macro offline_state() %}
          {% set domains = ['light', 'switch', 'sensor', 'binary_sensor', 'zwave', 'lock'] %}
          {% for domain in domains -%}
          {% for item in states[domain] if ((item.state | lower == "unavailable")) -%}
          {{ item.domain }} {{ item.name }} {%- if not loop.last %}, {% endif -%}
          {% endfor %}
          {%- endfor %}
          {% endmacro %}
          {{ offline_state() }}

- alias: offline_alert_clear
  trigger:
- platform: time
  minutes: '/30'
  seconds: 00
  condition:
- condition: template
  value_template: >
      {% macro offline_state() %}
      {%- set threshold = 40 -%}
      {% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
      {% for domain in domains -%}
      {% for item in states[domain] if ((item.state | lower == "unavailable")) -%}
      {{ item.domain }} {{ item.name }} {%- if not loop.last %}, {% endif -%}
      {% endfor %}
      {%- endfor %}
      {% endmacro %}
      {{ offline_state() |trim != "" }}
  action:
- service: persistent_notification.dismiss
  data:
    notification_id: offline-devices-alert
3 Likes

This is awesome. Thanks for sharing!

cool.
it so happens I’m using a variant of one of your templates:

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

I was just about to ask if I can substitute the fixed threshold for an input_number.battery_alert.

{%- set threshold = {{states('input_number.battery_alert')}} -%}

Nothing I try does the trick unfortunately. forgive me for posting here, dont want to hijack, and if no short answer is available, will take it elsewhere. Thought it to be very related though…

Check out the latest version on GitHub. It already has that feature.

Thank you for writing this code! I now also see that it needs some tweaking, but I will continue with my efforts. Learning everyday :slight_smile:

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