Help creating dynamic code for (new) sensors

Ok i give up, the yaml has me beaten :slight_smile:

What i want to do is create yaml code so it snags up all of my sensor.atc_* and shows them with a 2 row mushroom card, i’ll posta pic of how it looks.


In a perfect world i don’t want to have to edit anything when adding new sensors, no template editing no nothing.

The code that i’m using atm looks like something like this:

      - type: horizontal-stack
        cards:
          - type: custom:mushroom-template-card
            primary: Vind öster
            secondary: "{{states('sensor.atc_18da_temperature')}} °C | {{states('sensor.atc_18da_humidity')}}% | Batt: {{states('sensor.atc_18da_battery')}}%"
            icon: mdi:home-roof
            entity: sensor.atc_18da_temperature
            icon_color: >-
              {% set batt = states('sensor.atc_18da_battery')|int(0) %}
              {% if batt < 20 %}red{% elif batt < 50 %}orange{% else %}green{% endif %}
            tap_action:
              action: more-info
              entity: sensor.atc_18da_temperature
            badge:
              icon: mdi:battery

          - type: custom:mushroom-template-card
            primary: Vind väster
            secondary: "{{states('sensor.atc_e49c_temperature')}} °C | {{states('sensor.atc_e49c_humidity')}}% | Batt: {{states('sensor.atc_e49c_battery')}}%"
            icon: mdi:home-roof
            entity: sensor.atc_e49c_temperature
            icon_color: >-
              {% set batt = states('sensor.atc_e49c_battery')|int(0) %}
              {% if batt < 20 %}red{% elif batt < 50 %}orange{% else %}green{% endif %}
            tap_action:
              action: more-info
              entity: sensor.atc_e49c_temperature
            badge:
              icon: mdi:battery

I've been trying to get something usefull with a ai friend of mine but it's mostly not worked out, this is the best effort so far i guess:

cards:
  # Row 2: Dynamic ATC sensors
  - type: custom:auto-entities
    layout: horizontal
    filter:
      include:
        - entity_id: sensor.atc_*_temperature
    card:
      type: entities #custom:mushroom-template-card
      entity: sensor.atc_fake_temperature # dummy sensor; auto-entities replaces this with each matching sensor
      primary: >-
        {% set device = entity.entity_id | regex_replace(find='sensor\\.(atc_[^_]+)_temperature', replace='\\1') %}
        {{ state_attr(entity, 'friendly_name') if state_attr(entity, 'friendly_name') else device }}
      secondary: >-
        {% set device = entity.entity_id | regex_replace(find='sensor\\.(atc_[^_]+)_temperature', replace='\\1') %}
        {{ states("sensor." ~ device ~ "_temperature") }} °C | {{ states("sensor." ~ device ~ "_humidity") }}% | Batt: {{ states("sensor." ~ device ~ "_battery") }}%
      icon: mdi:car-garage
      icon_color: >-
        {% set device = entity.entity_id | regex_replace(find='sensor\\.(atc_[^_]+)_temperature', replace='\\1') %}
        {% set batt = states("sensor." ~ device ~ "_battery") | int %}
        {% if batt < 20 %}
          red
        {% elif batt < 50 %}
          orange
        {% else %}
          green
        {% endif %}
      tap_action:
        action: more-info
        entity: entity
      badge:
        icon: mdi:battery

commenting in mushroom dosen't work, with entities i at least see something (refering to this row=       type: entities #custom:mushroom-template-card )

I'm in over my head here, help? :slight_smile:

Correct formatting of your post.

I’d also like to see a good example of an automatic dashboard for OpenMQTTGateway-discovered sensors.

Note that now after you corrected the formatting, some not-code text is shown as a code.
So, what I see is 4 (could be more) cards of same structure but with different source entities, right?
And you want to NOT reappear same code several times, yes?

If you need to show same card (means - same structure) in different views or for entities which do not follow some naming pattern - consider decluttering-card: create a decluttering card template and then use it for different entities.
If you need to show these same cards in one view only and entities follow some naming pattern - this can be done by auto-entities (and mentioned decluttering template may be used as well).

P.S. The AI friend suggested you a wrong code for auto-entities. We may consider this auto-entities card if it is really needed.

How is your post related to this topic? Suggest to create a separate thread.

I see it as the same basic problem, auto generating a dashboard based on the names of sensor entities. It just so happens mine have a different naming pattern than OP, but the basic question is the same.

If you need to layout several SAME cards with different entities - then auto-entities could be a good option.
Basically, all auto-entities related questions are asked in the main dedicated thread:

Bu in this thread it could be not only auto-entities related question, let’s wait what OP says.

I don’t particularly care or have a prefrence to any given card, i’m just after a working solution :slight_smile:

The pattern of these sensors is:
sensor.atc_temperature
sensor.atc
humidity
sensor.atc
*_battery
stuff those values in a compact card as seen in the picture, 2 colums and then a new row with another 2 colums and repeat that until there are no more sensors.

I’m looking for someone thats better at this stuff than me that can help out and to learn more at the same time.

I’ll take a look at that declutter carad, maybe it makes it feasable to automate it in a sane way.
Thanks.

Are you waiting for a ready code? I think I already gave you a pointer.
What questions do you have?

I got further along with declutter, it works if i send in the sensors “manually” so it should be good, i’ll post my template and test code that works:

decluttering_templates:
  sensor_card:
    card:
      type: custom:mushroom-template-card
      primary: "[[primary]]"
      secondary: >
        {{states('[[temperature_sensor]]')}} °C | {{states('[[humidity_sensor]]')}}% | Batt: {{states('[[battery_sensor]]')}}%
      icon: mdi:home-roof
      entity: "[[temperature_sensor]]"

This code works, it produces stuff with values in the frontend:

  - type: horizontal-stack
    cards:
      - type: custom:decluttering-card
        template: sensor_card
        variables:
          - primary: blabla_sensor
          - temperature_sensor: sensor.atc_18da_temperature
          - humidity_sensor: sensor.atc_18da_humidity
          - battery_sensor: sensor.atc_18da_battery

Then i mixed in auto-entities and it “kinda” works, problem is that the variables don’t seem to “expand” so it looks ok but all the values are unknown. I’ve been trying permutations of {{ and [[ but can’t seem to find what’s wrong.

  - type: custom:auto-entities
    card:
      type: grid
      columns: 1
      square: false
    card_param: cards
    filter:
      include:
        - entity_id: sensor.atc_*_temperature
          options:
            type: custom:decluttering-card
            template: sensor_card
            variables:
              - primary: "[[entity.attributes.friendly_name]]"
              #- temperature: "{{states('[[entity_id]]')|state}}"
              - temperature: "[[ entity.entity_id|state ]]"
              - humidity: "[[entity_id|replace('_temperature', '_humidity')|state]]"
              - battery: "[[entity.entity_id|replace('_temperature', '_battery')|state]]"

I think i’m close, but that dosen’t really help… :slight_smile: