How to access elements of a group defined in Home Assistant

I have a group defined in groups.yaml, and one of them is “group.skal” which contains all my binary door/window sensors. Today I have a part of an app stating the below:

self.listen_state(self.skal, “group.skal”, new = “on”)

and then the corrosponding function:

def skal (self, entity, attribute, old, new, kwargs):
if self.get_state(“input_boolean.alarm_mode”) == “on”:
self.notify(“Doors or windows open”, name = “telegram_all”)

Which works fine, but I would much rather now which element withing my group that is “on”, so if it the front door, that I could have the message “Front door open” instad of “Doors or windows open”.

But how do I access the elements in the group? I would have thought a for loop, but I am unsure if that is the right path?

I think a for loop is the right way to do this, or at least I’m doing it like this and don’t have any problems :slight_smile:

Just do like this:

def initialize(self):
    for entity in "group.skal":
        self.listen_state(self.skal, entity, new='on')

    def skal(self, entity, attribute, old, new, kwargs):
        if self.get_state("input_boolean.alarm_mode") == 'on':
            self.notify(f"{entity} has been opened!", name="telegram_all")

Please use preformatted text to post your code the next time, otherwise it is difficult to read.

1 Like

This also works:

        group = self.get_state("group.philips_hue", attribute="entity_id")
        for entity in group:
            self.log(entity)
1 Like

Sorry, missed that you wanted to to list the “on” entities:

        group = self.get_state("group.philips_hue", attribute="entity_id")
        for entity in group:
            if self.get_state(entity) == "on":
                self.log(entity + " is on")
1 Like

Thanks for the reply - I get an error “2019-05-16 21:28:00.648224 WARNING AppDaemon: Alarm: Entity . not found in AppDaemon” in the log with the above code - but I can’t figure out why.

The app looks like this, if I remove the other parts of it - the old and the new version of “skal”:

import appdaemon.plugins.hass.hassapi as hass
import datetime

class alarm(hass.Hass):

  def initialize(self):
   self.listen_state(self.skal, "group.skal", new = "on")
   for entity in "group.skal":
        self.listen_state(self.skal2, entity, new='on')

  def skal (self, entity, attribute, old, new, kwargs):
    if self.get_state("input_boolean.alarm_mode") == "on":
        self.notify("Doors or windows!", name = "telegram_all")
       
  def skal2(self, entity, attribute, old, new, kwargs):
        if self.get_state("input_boolean.alarm_mode") == 'on':
            self.notify(f"{entity} has been opened!", name="telegram_all")

Thanks that works, but only when the entire group turns on, so if a door is opened first and then afterwards a window, only the door gets logged. I know that is also how it worked in my only example, but perhaps it could expanded further - or have I implemented your solution incorrectly?

Can you please put a log here and show the output?

    def initialize(self):
        for entity in "group.skal":
            self.log(entity)
            self.listen_state(self.skal2, entity, new='on')

The log is this:

2019-05-16 22:11:44.767956 INFO AppDaemon: Reloading Module: /config/appdaemon/apps/alarm.py
2019-05-16 22:11:44.775927 INFO AppDaemon: Initializing app Alarm using class alarm from module alarm
2019-05-16 22:11:44.781324 INFO Alarm: g
2019-05-16 22:11:44.785053 INFO Alarm: r
2019-05-16 22:11:44.787062 INFO Alarm: o
2019-05-16 22:11:44.789037 INFO Alarm: u
2019-05-16 22:11:44.791015 INFO Alarm: p
2019-05-16 22:11:44.792996 INFO Alarm: .
2019-05-16 22:11:44.793414 WARNING AppDaemon: Alarm: Entity . not found in AppDaemon
2019-05-16 22:11:44.795323 INFO Alarm: s
2019-05-16 22:11:44.797283 INFO Alarm: k
2019-05-16 22:11:44.799267 INFO Alarm: a
2019-05-16 22:11:44.801216 INFO Alarm: l

So it seems like it reads “group.skal” as a string?

You need to add listen_state to each member of the group in your initialize function to make sure our callback fires for whenever any of the members change state:

def initialize(self):
        group = self.get_state("group.skal", attribute="entity_id")
        for entity in group:
            self.listen_state(self.skal, entity, new='on')
2 Likes

Ah, I forgot, you can’t loop through the group directly, add this in the beginning
edit: @tjntomas was quicker :slight_smile:

class alarm(hass.Hass):

  def initialize(self):
      group = self.get_state("group.skal", attribute="entity_id")
      for entity in group:
          self.listen_state(self.skal2, entity, new='on')

  def skal (self, entity, attribute, old, new, kwargs):
      if self.get_state("input_boolean.alarm_mode") == "on":
          self.notify("Doors or windows!", name = "telegram_all")
1 Like

That’s it! Now it works - Thanks a lot!

Is there an easy way to get the friendly name of the entity - the notification says “binarysensor.openclose6 has been opened”.

This line can be omitted if you add a callback constraint in your app.yaml file:

constrain_input_boolean: input_boolean.alarm_mode

A callback constraint means that the callbacks in your app will only fire if, in this example, the constrain_input_boolean entity state is on. Check it out in the docs:https://appdaemon.readthedocs.io/en/latest/APPGUIDE.html#input-boolean

use the attribute “friendly_name” instead of the entity_id

friendly_name = self.get_state(entity, attribute="friendly_name")

and pass friendly_name instead of entity to the notify call.

1 Like

Great, it works like a charm now :grinning: