Filtered expand function includes some filtered out domains?

Hello HA community!

I’m having a problem with the expandfunction in Home Assistant. I’m trying to create a binary sensor that tracks whether any lights are on in a specific area. As such, this allows me to make an automation that set the lights to 30% based on motion, but only if the lights aren’t already on (thus inadvertently dimming my lights).

I’m using the expand function with the light domain and the area_entities function to get a list of the lights in that area. However, the expand function is also returning entities that belong to some other domains, specifically remote and media_player. This causes my binary sensor to return true, even though there are no lights on in that area.

Here is the value template that I’m using for my binary sensor:

{% set lights_on = expand('light', area_entities('Living / Werkkamer')) %}
{{ lights_on | selectattr('state', 'eq', 'on') | list | count > 0}}

This value template should return False if there are no lights on in the living / werkkamer area, and True otherwise.

However, when remove the count > 0 function to see the list of entities that are returned by the expand function, I get the following output:

[<template TemplateState(<state remote.living_room_tv=on; activity_list=None, current_activity=com.spotify.tv.android, friendly_name=Living Room TV, supported_features=4 @ 2024-02-14T19:30:12.612483+01:00>)>, <template TemplateState(<state media_player.living_room_tv_2=on; app_id=com.spotify.tv.android, app_name=com.spotify.tv.android, assumed_state=True, device_class=tv, friendly_name=Living Room TV, supported_features=22457 @ 2024-02-14T19:30:12.611817+01:00>)>, <template TemplateState(<state media_player.home_theater_2=on; source_list=['AUX', 'Blu-ray', 'Bluetooth', 'DVD', 'HEOS Music', 'Living Room TV', 'Mediabox', 'Phono', 'PlayStation 5', 'TV Audio', 'Tuner'], sound_mode_list=['MUSIC', 'MOVIE', 'GAME', 'AUTO', 'STANDARD', 'VIRTUAL', 'MATRIX', 'ROCK ARENA', 'JAZZ CLUB', 'VIDEO GAME', 'MONO MOVIE', 'DIRECT', 'PURE DIRECT', 'DOLBY DIGITAL', 'DTS SURROUND', 'AURO3D', 'AURO2DSURR', 'MCH STEREO', 'STEREO', 'ALL ZONE STEREO'], volume_level=0.335, is_volume_muted=False, media_content_type=channel, media_title=Living Room TV, source=Living Room TV, sound_mode=STEREO, sound_mode_raw=Stereo, dynamic_eq=False, device_class=receiver, friendly_name=Home Theater, supported_features=69004 @ 2024-02-14T19:30:14.966532+01:00>)>]

Put into a nice table, this means I’m seeing:

Entity ID Domain State
remote.living_room_tv remote on
media_player.living_room_tv_2 media_player on
media_player.home_theater_2 media_player on

As you can see, the expand function is returning entities that have the domain remote or media_player, which are not lights.

I have checked the following things to make sure that there are no entities that belong to the light domain in that area:

  • The area name that I’m passing to the area_entities function is correct and matches the one that I have configured in Home Assistant.
  • There are no entities that have the domain light in that area, either directly or indirectly. I have checked this by going to the Configuration menu, under the Areas section, and selecting the area that I’m using.
  • There are also no groups that contain entities that have the domain light in that area.

I’m not sure why the expand function is not working as expected. It should only return the entities that belong to the light domain, but it is also returning the entities that belong to other domains. This is why my binary sensor returns true, even though there are no lights in that area.

Is this a bug in the Home Assistant template engine or the expand function? Has anyone else encountered this problem? How can I fix it?

Any help or advice would be greatly appreciated. Thank you! :blush:

Your template, as you have already discovered, is not doing that. The initial light in expand is invalid and so it’s expanding everything in the area.

Assuming you have an area named Living / Werkkamer, this is how to check if any light entities in that area are on.

{{ expand(area_entities('Living / Werkkamer') | select('match', 'light'))
  | selectattr('state', 'eq', 'on') | list | count > 0 }}

To understand how it works, copy-paste the following template into the Template Editor and observe the results.

{{ area_entities('Living / Werkkamer') }}
-----------------------------------------------------
{{ area_entities('Living / Werkkamer') | select('match', 'light') | list }}
-----------------------------------------------------
{{ expand(area_entities('Living / Werkkamer') | select('match', 'light'))
  | selectattr('state', 'eq', 'on') | list }}

Hi @123 , thanks a lot for your response! I was actually going for that approach, but ended up making the wrong turn somewhere. Not sure how I was guided of path, perhaps mixed standard Jinja notation.

I did use an approach similar to your where I check for each step was the outcome was using the developer tools. It did work for my other areas, even though they have entities that can have an on/off state. Not sure why that worked then.

Your suggestion works perfectly! I’ve updated all my binary area sensors and it’s now working as expected. Thanks a lot for your help!

1 Like