How to show Dynamic Attribute from sensor

All,

What do I have?
I have HA setup, and the normal HA Integration for Sonarr. ( I do not use the HACS addons. ).
The sensors work, and when I look at sensor: sensor.sonarr_shows_2 I do see all my shows listed in the Attributes list.

I try to focus on: sensor.sonarr_queue_2. This sensor only has attributes when a download occures, and the look like:
-
Example:
Trafficked with Mariana van Zeller S02E08 - 8.2%

The problem I have with this is that the Attribute Name will always be different…

What do I want to achieve:
I want to have a conditional tile that shows only of there is a download, and is should show the name and percentage download.

I have created this:

      - type: conditional
        conditions:
          - entity: sensor.sonarr_queue_2
            state_not: '0'
        card:
          type: entities
          entities:
            - type: attribute
              entity: sensor.sonarr_queue_2
              attribute: Trafficked with Mariana van Zeller S02E08

This actually worked, it shows the percentage of download.

How do I make the “attribute: Trafficked with Mariana van Zeller S02E08” dynamic. So that it shows every episode and show that is downloading?

Not too much info to work ‘dynamic’ can mean anything to me … but maybe this is possible with a template and as the standard card do not accept templates you can use hacs “card templater”
I would first try to make sure you can ‘template’ this requirement before even trying with the card.

What i mean by dynamic is that the list can contain anything.

So currently the sensor sensor.sonarr_queue_2 is empty, because there is no download. When some TV Show is downloading an episode the sensor entity: sensor.sonarr_queue_2 shows a 1, because 1 episode is downloaden. The Attribute name can be anything… Like:

Trafficked with Mariana van Zeller S02E08 - 8.2%
or
The Blacklist S01E09 - 10%
or
Vikings S03E05 - 15%

With this code it got it working to catch 1 specific TV Show (Trafficked with Mariana van Zeller) / Episode (S02E08):

      - type: conditional
        conditions:
          - entity: sensor.sonarr_queue_2
            state_not: '0'
        card:
          type: entities
          entities:
            - type: attribute
              entity: sensor.sonarr_queue_2
              attribute: Trafficked with Mariana van Zeller S02E08

The Dynamic part is that the Attribute Name is an ever changing variable. Would it be possible to search for a wildcard, or ANY, in the attribute name?

Paste the content of your sensor with at least one attribute please, not sure if I can help but need tech data , screenshot is also fine (devtools > states)

I really appreciate your help, thank you.

Screenshot 1: No episode being downloaded

Screenshot 2: A episode being downloaded:

Like I said, the list of TV Shows and Episodes that are being downloaded are endless. And there could also be more then 1 download in the queue.

Is it always the first attribute?

I afraid it is not, I tried a new test met 3 episodes and it looks like this:

Does it always start with Traficked?

No… Every TV Show has a different name, like Blacklist, or Vikings etc. There is no logic there.

You are looking for a consistant? A logic to filter?

The only thing i see is:

  • It always ends with a % sign.
  • It always ends with #.##%
  • It always contains S##E##

Where # equals a number. Hope this helps

I am pretty sure that the guru’s have a more efficient solution but this may do the trick
Try it out in devtools > template

{% for key, value in states.[sensorname].attributes.items() %}
{% if '%' in value | string %}
{{ key }}: {{ value }}
{% endif %}
{% endfor %}

btw, you will probably need a different card
entities only has one row and cannot work with more than one attribute

Yes, I added it quickly to a Markdown Card, this worked perfectly, thanks!

Hey, do you know maybe how to create a custom sensor that has variable attributes?

what i have: several devices that have the name *valve_position
what I want to get: one sensor which will show the sum as state, and in the attributes will show the name of each device and its state

What I’ve managed to do so far:

template:
  - sensor:
    - name: Zawory
      unique_id: valvesPos
      state: "{{ states | selectattr('entity_id', 'search', 'valve_position') | map(attribute='state') | map('float') | sum }}"

what is missing:

      attributes:
        first_valve_position: someValue
        second_valve_position: someValue
        ...

p.s.

{
{% for i in states | selectattr('entity_id', 'search', 'valve_position') %}
{{ i.attributes.friendly_name }}: {{ i.state | int }}
{% endfor %}
}

shows proper(?) names and values:

but i don’t know how to use it to create attributes. It returns an error:

Invalid config for [template]: expected a dictionary for dictionary value @ data[‘sensor’][0][‘attributes’]. Got '{ {% for i in states | selectattr…

Never did that but do check here
Template - Home Assistant (home-assistant.io)

this shows predefined latitude and longitude attributes, with dynamic values, rather than a dynamic list of attributes

as mentioned, never done this before but I guess it won’t work with a template.
First of all, if the template would kick back the attribute and if (!) this would be accepted, then the template needs to reload to apply that new value (this is at least what I understand)… when reloading / restarting, I am not sure (doubt) if the template waits for the temlated part getting the attib name…all my thoughts, not 100% sure