Create Entity with variable attributes possible?

So in HASS Agent, the Primary Display sensor doesn’t have its own Resolution attribute, it just states which monitor is primary. However HASS Agent does provide the Resolution of each monitor currently active and connected in their own entity (eg. sensor.pc_display_display5 has an attribute with 2560x1440)

I’m trying to create a new entity with the state of the primary display, and also has the attribute of the monitor that is set to Primary. How would I write a template attribute that if, for example, detects display2 as primary, pulls the attribute from sensor.pc_display_display2? Preferably I don’t want to manually list each sensor.pc_display_display(number) as an if statement, so maybe a regex string would be best?

Show us a screenshot of the relevant entities in Developer Tools / States and explain with examples what you want to happen.

Template sensors are documented here:

and you’ll need YAML configuration if you want to use attributes, as the UI configuration does not yet support that.

You will be able to use a template to pull out the data you want, but I can’t suggest exactly what that template would be without seeing the structure of your existing entities. Template language is documented at the Jinja site and HA’s extensions — go to Developer Tools / Template (which will be a good place to try out your templates) and see the two links at the top.

Maybe something like:

template:
  - sensor:
      - name: Primary Display details
        state: "{{ states('sensor.primary_display') }}"
        attributes:
          resolution: >
            {{ state_attr('sensor.pc_display_display' ~ this.state, 'resolution') }}

I had OpenAI try to work a starter for me hopefully it’s better to understand:

- platform: template
  sensors:
    custom_resolution:
      friendly_name: "Nerdbox Resolution"
      value_template: >
        {% set entity_state = states('sensor.nerdbox_display_primary_display') %}
        {% set match = entity_state | regex_search('DISPLAY\([0-9]\)') %}
        {% if match %}
          {% set number = match.group(1) | int %}
          {% set sensor_entity_id = 'sensor.nerdbox_display_display' + number|string %}
          {% set resolution = states(sensor_entity_id) %}
          {{ resolution.attributes.Resolution if resolution.attributes.Resolution else 'Unknown' }}
        {% else %}
          'Invalid format or number out of range'
        {% endif %}

So I want to match the attribute it pulls from with the entity state value. So if the entity state is display1, it will pull the Resolution attribue from sensor.nerdbox_display_display(number from entity).

The code above doesn’t work, as it always returns the ‘else’ option.

There’s a surprise. Please don’t post AI-generated “code”: it usually sucks and is wrong in ways that takes a while to digest. This one is particularly poor: horribly invalid code, and uses the legacy template sensor format.

What would be better to understand is the screenshot I asked for. I can’t see what the state of sensor.nerdbox_display_primary_display looks like, for example, but I’m guessing from the robot vomit that it’s not a simple number.

Here’s a repeat of my guess above with the new entities you didn’t supply in the first post, and code to pull the number part out of the primary display sensor:

template:
  - sensor:
      - name: Primary Display details
        state: "{{ states('sensor.nerdbox_display_primary_display')|select('in','0123456789')|join }}"
        attributes:
          resolution: >
            {{ state_attr('sensor.nerdbox_display_display' ~ this.state, 'resolution') }}

I’ve posted the screenshot of the primary display sensor and the display1 attributes:


So basically I want to create a new sensor that shows which is the primary display currently as the state, and then inside the attributes ‘Resolution’ it pulls the attribute data from sensor.nerdbox_display_display(whatever number). I would prefer the number generation to be automatic or with regex if that’s possible. I have 3 monitors, and depending on what I’m doing the number changes and reflects in the primary sensor.

Thanks, that’s what I needed. Just a small tweak then:

template:
  - sensor:
      - name: Primary Display details
        icon: mdi:monitor
        availability: "{{ 'DISPLAY' in states('sensor.nerdbox_display_primary_display') }}"
        state: "{{ states('sensor.nerdbox_display_primary_display')|select('in','0123456789')|join }}"
        attributes:
          resolution: "{{ state_attr('sensor.nerdbox_display_display' ~ this.state, 'Resolution') }}"
          width: "{{ state_attr('sensor.nerdbox_display_display' ~ this.state, 'Width') }}"
          height: "{{ state_attr('sensor.nerdbox_display_display' ~ this.state, 'Height') }}"

The state of this sensor is the number part of the primary display sensor. That is used in the attribute definitions (as this.state) to pull the data from the appropriate display entity. No regex or ifs needed.

Am I placing it in the right location? I went into helpers and tried to create a template sensor but the values become unavailable…

See what I said earlier. You can’t do this via the UI. My code block will need to go in either configuration.yaml or templates.yaml (without the first line) depending how your config is set up, and you’ll need to restart.

You could create a template sensor via the UI with a state of the primary display resolution, if you wanted. Use a state template of:

{{ state_attr('sensor.nerdbox_display_display' ~ (states('sensor.nerdbox_display_primary_display')|select('in','0123456789')|join), 'Resolution') }}

This worked! Thank you.

I had to create a template.yaml and placed the code in there.

1 Like