Nested entities in templates

Hi, I’m just getting started with templates and would to know if there is a way to get to nested entities?

background.
I have 16 down lights in my kitchen which were not consistently reacting to commands - 16 ‘on’ commands at the same time was not reliable over ZHA. So, I created four groups and the scene now refers to the grouped zigbee lamps and is much more reliable.

But once in a while there are still bulbs that don’t react correctly, so I am writing a flow to check all the lights in a scene match the intended values - this will be triggered shortly after the scene is set.

To check if all the lights go off, I want to iterate over the entities in the scene, however when I run this template:

scene scene.kitchen_off
{% for item in state_attr('scene.kitchen_off','entity_id') %}
{{ item }} {% endfor %}

returns a mix of entities and groups:

scene scene.kitchen_off

switch.kitchen_cabinet_lights_switch_2 
light.zigbee_antenna_kitchen_spots_group_1 
light.zigbee_antenna_kitchen_spots_group_2 
light.zigbee_antenna_kitchen_spots_group_3 
light.zigbee_antenna_kitchen_spots_group_4 

Is there a way for a template to expand the list to show the full list of lamps, and not one lamp and four groups?

Bonus question - can I query the lamp state definitions stored in a scene (to compare with actual)?

regards
Steve

Try expand:


{{ expand('scene.kitchen_off') |map(attribute='entity_id') |list }}

Ad bonus question: As far as I know: no. What is your intention?

Are you getting error messages when the lights fail to turn off/on? If not, HA may think the change was successful, in which case I don’t think there’s any way of knowing the actual state of the light.

I am using mainly Hue lights with the ZHA integration. When a light fails to switch correctly the light’s history will usually track the incorrect state - sometimes after a few minutes.

In the following case, the lights all went out, but one came back on a few minutes later - I was in the room and can confirm they did all go out.

I already record the selected scene in a text helper, so comparing the scene definition with the actual lights is what I want to achieve.

Forgive me, but this seems like a very complex way to not address a problem… :smile:

If the history shows the light coming on again, and (if I understand you correctly) there are no errors… doesn’t that suggest that something switched it on legitimately? Might it not be better to track down what that was?

I posted another question on how to track lights coming on unexpectedly
https://community.home-assistant.io/t/how-to-trace-what-is-turning-on-a-light/677008/6

A more common issue is one light unchanged after calling a scene for a room with several lights. I have prototyped this flow in Node Red

This iterates over the lights and for any found on it tries to turn them off.

This won’t work so well for lamps that didn’t turn on as this flow does not capture the intended state (brightness, colour temp).

Expand didn’t work

1)
{{ expand('scene.kitchen_off') |map(attribute='entity_id') |list }}
2)
{{ expand('scene.kitchen_off') |map(attribute='entity_id') }}
3)
{{ expand('scene.kitchen_off') }}

resulted in

1)
['scene.kitchen_off']
2)
<generator object sync_do_map at 0x7f4b139140>
3)
[<template TemplateState(<state scene.kitchen_off=2024-02-03T08:18:50.312575+00:00; entity_id=['switch.kitchen_cabinet_lights_switch_2', 'light.zigbee_antenna_kitchen_spots_group_1', 'light.zigbee_antenna_kitchen_spots_group_2', 'light.zigbee_antenna_kitchen_spots_group_3', 'light.zigbee_antenna_kitchen_spots_group_4'], id=1696671468215, friendly_name=Kitchen Off @ 2024-02-03T08:18:50.312708+00:00>)>]

The intent is to compare the desired scene with what the actual values are - trying to correct the occasional glitch in setting a complex scene.

Sorry, tested it with a lightgroup, not with a scene. This should work:


{{ expand(state_attr('scene.kitchen_off', 'entity_id') ) |map(attribute='entity_id') |list  }}

@pedolsky thanks, but the template is not getting the entities I need:

it returns 4 groups and 1 light

['light.zigbee_antenna_kitchen_spots_group_4',
 'light.zigbee_antenna_kitchen_spots_group_3',
 'light.zigbee_antenna_kitchen_spots_group_2',
 'light.zigbee_antenna_kitchen_spots_group_1',
 'switch.kitchen_cabinet_lights_switch_2']

but I am trying to get the 17 individual bulbs:

['light.kitchen_spot_1_3_light',
'light.kitchen_spot_2_1_light',
'light.kitchen_spot_2_2_light',
'light.kitchen_spot_2_3_light',
'light.kitchen_spot_3_1_light',
'light.kitchen_spot_3_2_1_light',
'light.kitchen_spot_3_2_2_light',
'light.kitchen_spot_3_2_3_light',
'light.kitchen_spot_3_2_4_light',
'light.kitchen_spot_3_2_5_light',
'light.kitchen_spot_3_3_light',
'light.kitchen_spot_4_1_light',
'light.kitchen_spot_4_3_light',
'light.kitchen_spot_1_1_light',
'light.kitchen_spot_1_2_light',
'switch.kitchen_cabinet_lights_switch_2',
'light.kitchen_spot_4_2_light']

I’ve created a scene that contains 2 UI light groups. The code above shows me all the single members of those 2 groups .

Can you post a picture of the state of one of your groups (Developer Tools —> State)?

Ho @pedolsky, here is one of the groups:

It was created via the Zigbee Antenna device:

And then under Zigbee Home Automation

click on Configure > Groups

There are four groups in the kitchen. Each controls 4 of the 16 spots so that I can have some variation in the colours of the spots when I want.

These are the spots I want to check up on to see if they have turned on or off successfully.

That’s a Zigbee group.

In Home Assistant, it appears as a light entity. It doesn’t have an attribute listing the members of the Zigbee group so there’s nothing available for a template to access them.

As suggested in your other topic, you should focus on improving your Zigbee network’s reliability. Because if you are using Zigbee groups to control lights and still experience occasional failures of one or more lights then it implies there may be a problem with the network (because turning off 4 groups is just 4 broadcast messages and that’s very little traffic).

I think I understand what you are saying - there is no programmatic way I can query a ZHA group’s contents using home assistant even though the group was created through the HA UI?

If this is the case, then I’ll make a shadow scene that has all the entities directly assigned and iterate over that to validate the lights have been correctly set.

thanks’

Or focus your efforts on improving your Zigbee network so all of these extraordinary mitigation techniques aren’t needed.

1 Like