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
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.
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?
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')
Ah, I forgot, you can’t loop through the group directly, add this in the beginning
edit: @tjntomas was quicker
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")