Howto create battery alert without creating a template for every device

Interesting, I see exactly the same issue even though I added them to customise and then cleared the topics (sending an empty payload) but they keep being created.
If I click on one of them, even the battery_sensor_creation_disabled is showing as true
I am puzzled by that now.

battery_sensor_creation_disabled needs to be set on your Pressure Upstairs entity, not Pressure Upstairs Battery entity.

Thanks.
Iā€™ve tried every single entity ID and canā€™t make it work.
Everything with humidity/pressure I just added it customise , cleared the topics and itā€™s still showing up.
Pretty sure I am doing something wrong but canā€™t find what.

Can you go to the States panel (http://hassio.local:8123/dev-state), search for Pressure Upstairs in the attributes column, then post a screenshot? It should look something like the screenshot below.

Sure thing, here you go:

Add this to your customize section, restart Home Assistant, clear the retained topics, then restart Home Assistant one more time.

homeassistant:
  customize:
    sensor.pressure_158d0001a21e25:
      battery_sensor_creation_disabled: true

Just tried that and they are still showing up, unbelievable :slight_smile:
For the sake of it, I had all that on my custom.yaml (tried everything):

sensor.humidity_158d0001a21e25:
battery_sensor_creation_disabled: true
sensor.humidity_158d000171dfdc:
battery_sensor_creation_disabled: true
sensor.pressure_158d000171dfdc:
battery_sensor_creation_disabled: true
sensor.pressure_158d0001a21e25:
battery_sensor_creation_disabled: true

sensor.humidity_downstairs_battery:
battery_sensor_creation_disabled: true
sensor.humidity_upstairs_battery:
battery_sensor_creation_disabled: true
sensor.pressure_downstairs_battery:
battery_sensor_creation_disabled: true
sensor.pressure_upstairs_battery:
battery_sensor_creation_disabled: true

sensor.pressure_158d0001a21e25_battery:
battery_sensor_creation_disabled: true
sensor.humidity_158d0001a21e25_battery:
battery_sensor_creation_disabled: true
sensor.pressure_158d000171dfdc_battery:
battery_sensor_creation_disabled: true
sensor.humidity_158d000171dfdc_battery:
battery_sensor_creation_disabled: true

Hrm, thatā€™s odd. Can you confirm that your customize section is indented like the example I showed? Are you running the latest version from GitHub?

Yes, I am using 1.0.10 and the indentation is the same. I use a custom.yaml file but I know it is being read as I have other configurations there.
Very weird, indeed.

sensor.humidity_158d0001a21e25:
 battery_sensor_creation_disabled: true
sensor.humidity_158d000171dfdc:
 battery_sensor_creation_disabled: true
sensor.pressure_158d000171dfdc:
 battery_sensor_creation_disabled: true
sensor.pressure_158d0001a21e25:
 battery_sensor_creation_disabled: true

Can you check the attributes of sensor.pressure_158d0001a21e25 on the States panel after you changed customize? You should see battery_sensor_creation_disabled in the attributes column if customize worked. it should look like this:

Almost there ( I think!)
By doing what you asked, I confirmed the entry was not there as I was using those ones on its own custom file (xiaomi.yaml) so I added them:

So then I sent an empty payload to sensor,pressure and sensor.humidity for both sensors and also on config/state.

So:

1530225005: Received PUBLISH from hass (d0, q0, r0, m0, ā€˜homeassistant/sensor/hunidity_158d000171dfdc_battery/stateā€™, ā€¦ (0 bytes))
1530225010: Received PUBLISH from hass (d0, q0, r0, m0, ā€˜homeassistant/sensor/humidity_158d000171dfdc_battery/stateā€™, ā€¦ (0 bytes))
1530225013: Received PUBLISH from hass (d0, q0, r0, m0, ā€˜homeassistant/sensor/humidity_158d000171dfdc_battery/configā€™, ā€¦ (0 bytes))
1530225029: Received PUBLISH from hass (d0, q0, r0, m0, ā€˜homeassistant/sensor/pressure_158d000171dfdc_battery/configā€™, ā€¦ (0 bytes))
1530225033: Received PUBLISH from hass (d0, q0, r0, m0, ā€˜homeassistant/sensor/pressure_158d000171dfdc_battery/stateā€™, ā€¦ (0 bytes))
1530225054: Received PUBLISH from hass (d0, q0, r0, m0, ā€˜homeassistant/sensor/humidity_158d0001a21e25_battery/stateā€™, ā€¦ (0 bytes))
1530225061: Received PUBLISH from hass (d0, q0, r0, m0, ā€˜homeassistant/sensor/pressure_158d0001a21e25_battery/stateā€™, ā€¦ (0 bytes))
1530225065: Received PUBLISH from hass (d0, q0, r0, m0, ā€˜homeassistant/sensor/pressure_158d0001a21e25_battery/configā€™, ā€¦ (0 bytes))
1530225070: Received PUBLISH from hass (d0, q0, r0, m0, ā€˜homeassistant/sensor/humidity_158d0001a21e25_battery/configā€™, ā€¦ (0 bytes))

Saw the battery levels showing unknown on the UI and restarted but they are still showing.
Appreciate the help, btw!

Awesome. That looks better.

Did you also have retain flag set when you sent null payload?

mosquitto_pub -h MQTTSERVER -p 1883 -u MQTTUSER -P 'MQTTPASSWORD' -t "homeassistant/sensor/pressure_158d0001a21e25_battery/config" -r -n
mosquitto_pub -h MQTTSERVER -p 1883 -u MQTTUSER -P 'MQTTPASSWORD' -t "homeassistant/sensor/pressure_158d0001a21e25_battery/state" -r -n

After clearing the topics, you can also try re-runing mosquitto_sub to verify theyā€™re gone.

1 Like

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: