Dyanmically referencing self.listen_state

I thought I could figure this out, but it may be that this is specific to AppDaemon’s docs and not python in general.

I am writing out a program for notifications when a door opens. Instead of writing out a function for each door, I’m having the initialize function look through multiple sensors and then for loop through the sensors. Here’s the code:

   def initialize(self):
      self.doors = ['binary_sensor.den_door', 'binary_sensor.sun_room_door',
                     'binary_sensor.front_door', 'cover.garage_door']
      for door in self.doors:
            self.listen_state(self.door_status, new="on")

What I want to do is in the functions beyond this, I want to reference the entity that triggered the self.listen_state so I can pull the friendly name, or various attributes. But I can’t seem to figure out how to get that! Here’s a variable where I want to pull in the entity.

entity = self.entities.ENTITY.attributes.friendly_name

I’ve tried a few things, such as (kwargs[entity_name], door and doors, and even tried tying the listen_state in the initialize function to a variable. I feel like this should be a fairly simple solution, but I haven’t figured it out yet… any tips?

You are almost there:

   def initialize(self):
      self.doors = ['binary_sensor.den_door', 'binary_sensor.sun_room_door',
                     'binary_sensor.front_door', 'cover.garage_door']
      for door in self.doors:
            self.listen_state(self.door_status,  door, new="on") # You forgot to reference the entity in "door"
   def door_status(self, entity, attribute, old, new, kwargs):
        # The parameter "entity" contains the entity_id that triggered the callback.
        # Use get_state to get the friendly_name attribute
        self.log(self.get_state(entity, attribute = "friendly_name")) # This prints the friendly name to the log.

Oh my. :see_no_evil: I kept reading through the attributes in the documentation and when I read: entity: Name of the entity the callback was requested for or None., I for some reason wasn’t linking the self.listen_state as the callback function. I didn’t link the definitions, foolishly.

I appreciate your help! This worked perfectly.

Perhaps you know even more about AppDaemon, and if you’d permit me one more question… what is the different purpose and usage of getting entity information using
self.entities.entity.attributes.friendly_name as opposed to using self.get_state(entity, attribute="friendly_name").

In the above pieces of code, I tried using the former (the one from my post’s example), and it returns the error AttributeError: 'StateAttrs' object has no attribute 'entity' while your suggestion worked perfectly. What am I missing?

Great that you got it working!

self.entities.entity was introduced in later versions of Appdaemon. They do the same thing but you need to use get_state to reference by parameter.

To use self.entities, try this:

self.log(self.entities.sun.sun.attributes.elevation) # Prints the elevation of the sun.

so replace entity with the entity_id you want to look at.

This lists all your input_booleans:

        all_input_booleans = self.entities.input_boolean
        for entity in all_input_booleans: