Help: I'm stuck trying to get a count of 2 different entity types

Hi
I have some logic in some of my lighting automations that triggers lighting switch off faster when there’s only 1 person present. Which works great, but if there’s only 1 person here + we have a guest staying then there will be people showering in the dark.
So I was trying to modify my template sensor to also track when a guest is present and count them into my “someone is here” sensor.

Our guest mode is a boolean triggered either manually or by a calendar event
Resident presence is detected by the normal device_tracker.

I’m stuck trying to join them together:

        {{ states.device_tracker
        | rejectattr('attributes.friendly_name', 'eq', 'Wallscreen-TABLET')
        | selectattr('state', 'eq', 'home')
        | list | count }}
        {{ states.input_boolean
        | selectattr('attributes.friendly_name', 'eq', 'Guest Mode')
        | selectattr('state', 'eq', 'on')
        | list | count }}

The closest I got to was this:

{{ states | selectattr('entity_id', 'in', 'device_tracker.mobile1,device_tracker.mobile2,device_tracker.mobile3,device_tracker.mobile4, input_boolean.guest_mode')
| rejectattr('attributes.friendly_name', 'eq', 'Wallscreen-TABLET')
| selectattr('state', 'in', 'on, home')
|list|count }}

However it makes more sense to have new tracked devices/people be included automatically rather than having to manually update the sensor every time a new one is added, but I’m stuck on how to write this in jinja.

Does this work?

        {{ states.device_tracker
        | rejectattr('attributes.friendly_name', 'eq', 'Wallscreen-TABLET')
        | selectattr('state', 'eq', 'home')
        | list | count +
        states.input_boolean
        | selectattr('attributes.friendly_name', 'eq', 'Guest Mode')
        | selectattr('state', 'eq', 'on')
        | list | count }}

Or If you only have the one guest mode input_boolean:

        {{ states.device_tracker
        | rejectattr('attributes.friendly_name', 'eq', 'Wallscreen-TABLET')
        | selectattr('state', 'eq', 'home')
        | list | count 
        +  iif(is_state('input_boolean.guest_mode', 'on'), 1, 0) }}
1 Like

Copy-paste typo; remove the {{ from the fifth line.

1 Like

Done. Thanks.

The normal approach to seeing how many people are present in a zone is currently to use the state of the zone, which counts the people there by itself. Counting people makes more sense to me than counting device trackers, as one person may have more (I do), and not all device trackers may represent people (car, …). So if you introduce guests as a person into HA, wouldn’t the zone state already do what you want?

Yes, thank you. Both worked.
I tried all kinds of stuff similar to your #1 , but I didn’t try just adding a +

@Edwin_D Yes, counting per person/zone is probably better logic as long as it can still monitor each person present at home, but thanks to @tom_l I should be able to whip something up :slight_smile:

True. And you can also create a device tracker based on the value of the input boolean, and link a dummy guest person to it. Many roads lead to Rome…

Set for now as this.
Thanks all

        {{ states.person
        | rejectattr('attributes.friendly_name', 'eq', 'KIOSK')
        | selectattr('state', 'eq', 'home')
        | list | count 
        +  iif(is_state('input_boolean.guest_mode', 'on'), 1, 0) }}
1 Like

FWIW, the abs filter can convert boolean values into integers (true = 1 and false = 0). We can leverage that feature to convert an input_boolean’s state into an integer value.

So this part of the template posted above

+ iif(is_state('input_boolean.guest_mode', 'on'), 1, 0)

could be replaced by this

+ is_state('input_boolean.guest_mode', 'on') | abs
2 Likes

Neat. I did not know that.