How do I use Group.yaml in as a sensor

Hi
I’m a little fustrated atm as I’ve been trying to use a group I call kitchen_motion to trigger my automation in AppDaemon.
but it doesn’t report any errors nor does it work.

Here is how I’m using it:

class light_on_motion(hass.Hass):
    def initialize(self):
        self.log("Method initialize was called in the class light_on_motion")
        self.default_light_level_threshold = float(20)
        self.yeelight_specialcase = self.args["yeelight_specialcase"][0]
        self.get_up_time = "08:00:00"
        self.luxsensor = self.args["lux_sensor"][0]
        self.bedtime = "20:00:00"
        self.lights = self.args["lights"]
        self.handles =[]
        trigger = self.args["sensors"]
        self.listen_state(self.AutoLightOnMotion,trigger,new="on")

here is where I’m giving the group in my Apps.yaml

Kitchen:
  module: lightonmotion
  class: light_on_motion
  sensors:
    - kitchen_motion
  lux_sensor:
    - kitchen_lux_sensors
  lights:
    - light.kitchen_ikea_light_3
    - light.kitchen_ikea_light_2
    - light.kitchen_ikea_light_1
  yeelight_specialcase:
    - light.yeelight_color_0x7e73c4d

here is my group.yaml

kitchen_sensors:
  name: kitchen_motion
  entities:
    - binary_sensor.first_motion_sensor_motion
    - binary_sensor.kitchen_motion_2_motion
    - binary_sensor.kitchen_motion_1_motion

I hope somone can tell me what I’m doing wrong ? it works if I only set a list of sensors in to the apps.yaml but not when I give it a group.

The state of a group is “off” unless all members of the group are “on”, so all your motion sensors need to be “on” at the same time to trigger listen_event.

Humm. Group - Home Assistant says the opposite, the group state is on if at least one is on. Unless overridden by setting all.

I know this because use groups for lights and want the all condition for coloring icons.

You are right. I use groups with the “All entites” which reverses the function.

It is because you’re essentially passing in [“kitchen_motion”] as the entity ID to the listen_state() which is not a valid entity ID. So the callback never fires.

1 Like

maybe a noob question but if I can’t do it this way then what would I give the listen_state() to make sure it triggers when any of the sensors detect activity?

You have to give it the full entity name. In your case it might be “group.kitchen_motion”. The listen_state() method is supposed to accept strings and lists but please understand the behavior of that. If you pass in a list of entities to listen_state(). It will run the same callback when the state changes for any of the entities that are in the list. You’re currently passing in a single item list.

1 Like

You can create a template binary sensor to active your app like so

- platform: template
  sensors:
    people_home:
      device_class: occupancy
      value_template: >-
        {{ is_state('person.travis', 'home')
            or is_state('person.conrad', 'home')
            or is_state('person.victoria', 'home')
            }}

OR

you can listen to each sensor directly with a for loop like so

for sensor in sensors:
  self.listen_state(self.change_detected, sensor)

    def change_detected(self, entity_id:str, attribute,  old: str, new: str, kwargs = None) -> None:
        """Double checks things whenever there is a change"""
        name: str = self.friendly_name(entity_id)  # gets the name of the entity
        self.log(
            f"__function__: {entity_id} changed from {old} to {new}", level='DEBUG')
        if old != new:
            self.log(f"__function__: {name} changed from {old} to {new}")

so if I want it to only run when the group state changes then I’m doing it wrong ?

my requirements could be summed up like this:

scenario Turn on lights
given we have multiple sensors in the group
when any sensors state is changed from inactive to active.
then turn on the lights

scenario turn off lights
given we have multiple sensors in the group
when the sensors state is changed from active to inactive.
And all other sensor state in the group is inactive.
then turn off the lights

It is easiest just to listen to the group entity. That is the reason you create a group in the first place. You can also listen to each individual entity as well. It is totally up to you.

I thought I was listening to the group entity, which didn’t seem to work. Which is why I’m asking for help as I could not find anything in the documentation of appdaemon or home assistant to suggest what I was doing wrong.

According to your YAML, you have specified your sensor as “kitchen_motion” but that is not a valid entity. If it is a group then the entity is going to be “group.kitchen_motion”. That is the main issue right now so I’d work from there.

I tried that when you suggested it the first time. But I guess I wasn’t clear about that in my subsequent question for clarification. Sorry.

Your group name is group.kitchen_sensors not group.kitchen_motion.

I recommend you getting used to using the developer tools → states page to find entities and their id’s.

1 Like

I would agree with this assessment. You wont have much luck automating with AppDaemon without understanding how things are done in HA.

I ended up updating and make use of the group you can make in the helpers interface.
That solved my issue with not getting access to my groups in Appdaemon. now it seems to work the way I intended.