How to make a "platform: template" entity with attributes?

I am using an Elgato stream deck, and I can display multi-line output for a single button… but only if the data comes from the attributes of a single sensor. This works with yahoofinance and rest platform entities, but I can’t figure out how to do it with a template platform entity. This code creates four separate entities:

  - platform: template
    sensors:
      pge:
        friendly_name: "PG&E"
        value_template: >-
          {{ states('sensor.eagle_200_meter_power_demand') | int }}
      battery:
        friendly_name: "Batt%"
        value_template: >-
          {{ states('sensor.solaredge_storage_level') }}
      generation:
        friendly_name: "Self Consumption"
        value_template: >-
          {{ states('sensor.solaredge_storage_power') }} 
      sun:
        friendly_name: "Sun Power"
        value_template: >-
          {{ states('sensor.solaredge_solar_power') | int }}

Instead I’d like one entity with something like energy.pge, energy.battery, etc.

Is this possible?

Thanks,

Richard

2 Likes

I believe you can as long the attribute is available.

This Template Sensor will have attributes named battery, generation, and sun.

  - platform: template
    sensors:
      pge:
        friendly_name: "PG&E"
        value_template: "{{ states('sensor.eagle_200_meter_power_demand') | int }}"
        attribute_templates:
          battery: "{{ states('sensor.solaredge_storage_level') }}"
          generation: "{{ states('sensor.solaredge_storage_power') }}"
          sun: "{{ states('sensor.solaredge_solar_power') | int }}"

That’s not how it works in Home Assistant. To get the value of an entity’s attribute, the preferred method is to use the state_attr() function (see this section of the Templating documentation).

{{ state_attr('sensor.pge', 'battery') }}

The alternative method is:

{{ states.sensor.pge.attributes.battery }}
5 Likes

I’ve only ever used template sensors to break attributes out of other sensors but according to the documentation it is possible. Should be something like this:

  - platform: template
    sensors:
      pge:
        friendly_name: "PG&E"
        value_template: >-
          {{ states('sensor.eagle_200_meter_power_demand') | int }}
        attribute_templates:
          battery: "{{ states('sensor.solaredge_storage_level') }}"
          generation: "{{ states('sensor.solaredge_storage_power') }}"
          sun: "{{ states('sensor.solaredge_solar_power') | int }}"

EDIT: too slow.

3 Likes

hahahah… you are the man though…

Thank you, worked like a charm.