Dynamic selection of which entity to show using a template (in lovelace dashboard)

I spent a while looking for an easier way to dynamically select an entity to show in a dashboard based on a template (i.e. based on other state/computation) – an indirect reference to an entity, if you will. My use case was that I have sensors that track the entity_id of the camera of each of the last 5 recognized objects from my 30+ cameras and want to show a custom card for each of those 5 cameras in that sliding window of events. Using 5 condition cards and repeating the 30 cameras each time could’ve worked but obviously would’ve needed to be YAML that I generated programmatically (which I do plenty, but seemed wrong here).

This is what I came up with using custom:auto-entities's template and custom:layout-card to turn the entities: list from auto-entities into a single custom card of my choosing for the dynamically-selected entity.

          - type: custom:auto-entities
            card:
              type: "custom:layout-card"
            card_param: cards
            filter:
              template: >-
                {{{ "entity": state_attr('sensor.camect_latest_event', 'cam_entity_id'),
                  "type": 'custom:camect-card' }}},

I’m posting to both share this technique – which seems plenty general and useful – and also to ask if there’s a better way. I’m pretty confident that there should be a better way as simple things should be simple and although I’ve made this fairly idiomatic, it’s nuanced compared to just being able to specify an entity with a template directly.

Feedback?

Other options:

State switch card (switches whole card, not just a row, based on a condition): https://github.com/thomasloven/lovelace-state-switch

Lovelace templates (use templates in an entities card): 📝 100% Templatable Lovelace Configurations

Thanks for the pointers!

can u help me get this to work

i would like to get all cameras to send objects detected to HA
so i can trigger stuff

im using the
When camect_event event is fired

Template condition

Value template *

1 “person” in trigger.event.data.detected_obj

2

i have tried this too
“Side Door just saw a person” in camect_event.data

here is the data

event_type: camect_event
data:
device_id: camera.camect_5f5
type: alert
raw_data: >-
{‘type’: ‘alert’, ‘desc’: ‘Side Door just saw a person.’, ‘url’:
Camect’,
‘cam_id’: ‘2b817378d3af3e3d752b’, ‘cam_name’: ‘Side Door’, ‘detected_obj’:
[‘person’], ‘roi’: {‘contour’: [{‘point’: [{‘x’: 0.2375, ‘y’: 0}, {‘x’:
0.2375, ‘y’: 0.53333336}, {‘x’: 0.4125, ‘y’: 0.51111114}, {‘x’: 0.4125, ‘y’:
0}]}], ‘object’: [{‘name’: ‘person’, ‘min_size’: 0.034899354, ‘max_size’:
0.07909248, ‘contour’: [{‘point’: [{‘x’: 0.2375, ‘y’: 0}, {‘x’: 0.2375, ‘y’:
0.53333336}, {‘x’: 0.4125, ‘y’: 0.51111114}, {‘x’: 0.4125, ‘y’: 0}]}],
‘movement_trace’: {‘point’: [{‘x’: 0.3076482, ‘y’: 0.21135524}, {‘x’:
0.31138512, ‘y’: 0.19331792}, {‘x’: 0.31777573, ‘y’: 0.21104303}, {‘x’:
0.30945665, ‘y’: 0.22797905}, {‘x’: 0.33446985, ‘y’: 0.22949821}, {‘x’:
0.32112664, ‘y’: 0.26462772}, {‘x’: 0.3138296, ‘y’: 0.26877564}, {‘x’:
0.31674272, ‘y’: 0.26344457}, {‘x’: 0.3476174, ‘y’: 0.2664235}, {‘x’:
0.34987032, ‘y’: 0.2688015}, {‘x’: 0.34834647, ‘y’: 0.26507357}, {‘x’:
0.35948697, ‘y’: 0.26261866}, {‘x’: 0.35871056, ‘y’: 0.26328072}, {‘x’:
0.35777068, ‘y’: 0.26720345}]}}]}}
origin: LOCAL
time_fired: “2023-01-24T01:55:10.289103+00:00”
context:
id: 01GQGQRZAHWEZAP2C9C1AGJ3HH
parent_id: null
user_id: null