Automations using groups

Hi all,

Probably a basic question but I couldn’t find anything relevant while searching. What I want to do is create an automation to send me a notification anytime any one of my zwave devices’ battery is low. I have it working for one device and can enumerate all devices, but I’d rather be smarter and use a group if possible. How can I have the condition (trigger is time based at noon everyday) be based on any of the devices in group.zwave?

Thanks in advance!

Have you created a group named group.zwave, because I don’t think one is automatically created?

Can you post the existing automation for one of your zwave devices? That will help.

But basically, if you have a group that contains the entity_id’s of all the zwave devices you care about, this can definitely be done. Once I see one of your existing automations I can make a suggestion of how to do this.

You probably want to look at this thread.

2 Likes

I think the forum search fuction doesn’t work for you. Please try this.

Yes I have created a group of all of my zwave devices. It includes devices that aren’t battery powered, so not sure if that will hurt anything. I can always create a battery only group.

- id: '1530826484530'
  alias: Low Battery Warning
  trigger:
  - at: '12:20:00'
    platform: time
  condition:
  - below: '25'
    condition: numeric_state
    entity_id: zwave.vision_zp3111_multisensor_4in1
    value_template: '{{ state.attributes.battery_level }}'
  action:
  - data:
      message: A device battery is low
    service: notify.pushbullet

Thanks for the help! Much appreciated.

EDIT: I responded before I saw Tinkerer’s response.
EDIT 2: Actually so Tinkerer’s response solves my problem however I still don’t know how to use groups in automations so that would be immensely helpful to know for other things. I think it’s just using For loops though.

Thank you Tinkerer! This will work great

I was trying to search on how to use groups in automations, not specifically creating a battery alert.

No problem. Let’s see…

- id: '1530826484530'
  alias: Low Battery Warning
  trigger:
    at: '12:20:00'
    platform: time
  condition:
    condition: template
    value_template: >
      {{ states.zwave
         |selectattr('entity_id','in',state_attr('group.zwave_devices','entity_id'))
         |selectattr('attributes.battery_level','lt',25)|list|count > 0 }}

Hmm, I guess I’ll stop there. If the other solution doesn’t work out for you, and this seems interesting, let me know and I’ll complete the picture. :slight_smile:

1 Like

Thank you! I kind of understand what you’re doing there. I’m not sure what’s left though so if you want to complete it that would be really helpful for my own understanding.

Sure.

- id: '1530826484530'
  alias: Low Battery Warning
  trigger:
    at: '12:20:00'
    platform: time
  condition:
    condition: template
    value_template: >
      {{ states.zwave
         |selectattr('entity_id','in',state_attr('group.zwave_devices','entity_id'))
         |selectattr('attributes.battery_level','lt',25)|list|count > 0 }}
  action:
    service: notify.NOTIFIER_NAME
    data_template:
      title: Low Battery Alert
      message: >
        Devices with low battery:
        {%- for e in states.zwave
             |selectattr('entity_id','in',state_attr('group.zwave_devices','entity_id'))
             |selectattr('attributes.battery_level','lt',25) -%}
          {% if loop.first %} {% elif loop.last %} & {% else %}, {% endif %}{{ e.name }}
        {%- endfor %}

This, of course, assumes that all these entities have a battery_level attribute. If not, then it can be adjusted accordingly.

1 Like

Again thank you for this. Super helpful. One last thing is how come the conditions template doesn’t need a for loop to run through?

states is basically a generator. The selectattr filters just select specific inputs and output another generator. I then turn the output of the last generator into a list, and then the last filter results in a count of how many elements are in the list, which I then just simply compare to zero.

The action, however, needs a for loop because we actually want to do something with each entity, which is to append its name to the message string, as well as include appropriate punctuation between names. If we just wanted a simpler output, say a comma separated list, that could be done without a for loop using the map and join filters. E.g.:

  action:
    service: notify.NOTIFIER_NAME
    data_template:
      title: Low Battery Alert
      message: >
        Devices with low battery: {{
          states.zwave
          |selectattr('entity_id','in',state_attr('group.zwave_devices','entity_id'))
          |selectattr('attributes.battery_level','lt',25)
          |map(attribute='name')|join(', ') }}
1 Like

Ahh perfect explanation. I can’t thank you enough, you’ve deepened my understanding quite a bit!

1 Like