How to select entities for a card dynamically

Let’s consider a simple example.
There is an "input_select" control filled with persons’ names.
And below there is some card with some entities.
image

type: vertical-stack
cards:
  - type: entities
    entities:
      - entity: input_select.persons_list
  - type: glance
    entities:
      - sensor.cleargrass_1_co2
      - sensor.cleargrass_2_co2

In this example entities for the card are “hard-coded”.
Now I need to select entities dependingly on that "input_select" choice.
Kind of this:

  • if states(‘input_select.persons_list’) == “Ирина” then entities are: sensor.cleargrass_1_co2, sensor.cleargrass_2_co2;
  • if states(‘input_select.persons_list’) == “Ильдар” then entities are: sun.sun, binary_sensor.updater;
    and so on…

What is the best practice for this?

The auto entities card, template card, or some other custom card. Without custom, you’d be using conditional card for each input select value.

yeah, I would say the State-switch card by Thomas was designed with this purpose in mind.

Thanks a lot, I will start looking at these options!

Conditional card is cumbersome. I’d just go with a template-card or the state-switch card. Leaning towards state-switch card.

Do you mean config-template-card?
On the 1st look, the state-switch could be a good solution.
But I was thinking about reducing a code: it will be ONE card (some decluttering template) but with different sets of sensors.

So, something like this in pseudo code:

sensors[] =
{
  {sensor_11,sensor_12,sensor_13,sensor_14},
  {sensor_21,sensor_22,sensor_23,sensor_24},
  {sensor_31,sensor_32,sensor_33,sensor_34}
}  
switch(input_select_selection)
{
  case choice_1: i=0;
  case choice_2: i=1;
  case choice_3: i=2;
  case choice_4: i=3;
}
show_some_card(sensors[i])

Also, I was thinking about Lovelace gen.

The state switch card also has the advantage of not leaving a gap in a column of cards when nothing is shown, unlike the conditional card.

I use Lovelace gen but it does not have access to the state machine. I.e you’ll be using the conditional card with Lovelace gen

I don’t remember the name of the card, it’s the one made by iantrich

I guess petro means this one: GitHub - iantrich/config-template-card: 📝 Templatable Lovelace Configurations

Yes, this is config-template-card.
Managed to use "state-switch", it is great!
Thanks to everyone for advices!

title: Life360 (краткое)
path: life360-brief
icon: 'mdi:account'
badges: []
cards:
  - type: vertical-stack
    cards:
      - type: entities
        entities:
          - entity: input_select.persons_list
            name: Персона

      - type: custom:state-switch
        entity: input_select.persons_list
        default: "Ирина"
        transition: flip
        transition_time: 500
        states:
          "Ирина":
            type: 'custom:decluttering-card'
            template: decl_placeinfo_map_row_logbook
            variables:
              - __SENSOR_PLACEINFO: sensor.placeinfo_irina
              - __SENSOR_UPDATE_ALERT: binary_sensor.life360_update_alert_irina
              - __SENSOR_BATTERY_CHARGING: binary_sensor.battery_charging_life360_iphone_ira
              - __SENSOR_BATTERY: sensor.battery_life360_iphone_ira
              - __DEVICE_TRACKER: device_tracker.life360_iphone_ira

          "Ильдар":
            type: 'custom:decluttering-card'
            template: decl_placeinfo_map_row_logbook
            variables:
              - __SENSOR_PLACEINFO: sensor.placeinfo_ildar
              - __SENSOR_UPDATE_ALERT: binary_sensor.life360_update_alert_ildar
              - __SENSOR_BATTERY_CHARGING: binary_sensor.battery_charging_life360_ildar_iphone_5s
              - __SENSOR_BATTERY: sensor.battery_life360_ildar_iphone_5s
              - __DEVICE_TRACKER: device_tracker.life360_ildar_iphone_5s
....
1 Like

Not sure if it’s new, but this is where the type: conditional is perfect! You give it a condition for when to show and a row for what to show.

My Case: only show date field X if the stage X has been passed.

type: entities
entities:
  - type: conditional
    conditions:  # only if it has started blooming
      - entity: input_select.plant_stage
        state: Flower
    row:  # show the flower open date
      entity: input_datetime.big_tent_flower_start
  - entity: input_select.plant_stage
  - entity: input_number.week
  - entity: input_number.gallons
view_layout:
  position: sidebar