Create a modern sensor template with attributes NOT in templates

sensor:
- name: solar_aktuellestrahlung_einfallswinkel
  unique_id: solar_aktuellestrahlung_einfallswinkel
  icon: mdi:solar-power-variant-outline
  unit_of_measurement: W/m²
  state: >      
  		 {%        set Albedo = 0.2 %}
                 …
  		{{ (Direktstrahlung_geneigt + Diffusstrahlung_geneigt +                      Bodenreflexion_geneigt) |float |round (0) }}

Works without problems.

I want some attributes, that derive from the long template and the variables created.
But the problem is, that attributes has a different rendering compared to state and the sensor with attributes does not render and is not available

sensor:
- name: solar_grandl_math
  unique_id: solar_grandl_math
  icon: mdi:solar-power-variant-outline
  state:  >
                 {%        set Albedo = 0.2 %}
                  …
  		{{ (Direktstrahlung_geneigt + Diffusstrahlung_geneigt +                      Bodenreflexion_geneigt) |float |round (0) }}
   
    attributes: >
    {% set Albedo = 0.2 |float -%} Albedo: {{ Albedo  }}

Invalid config for 'template' at template/solar_grandl_math.yaml, line 9: expected a dictionary for dictionary value 'sensor->0->attributes', got ' {% set Albedo = 0.2 |float -%} Albedo: {{ Albedo }}'

Even the creation of a dictionary did not solve the error.

My mistake?

I assume this been cut down for brevity, otherwise it makes no sense at all.

To create an attribute like you are trying to, this should work (and no need for the float):

    attributes:
      Albedo: >
        {% set Albedo = 0.2 %}
        {{ Albedo }}

The template rendering is the same as for state: for each attribute, not for attributes: as a whole.

image
image

I’m not sure that answers your question, though.

Post the full config and explain what you want.

1 Like

Hey, that was a very fast answering, thank you!

My plan was to reuse the long calculation as a macro.
This macro can return the status
AND it can return a dictionary like structure or two lists.
Create the attributes with {% for key, value in dict %)

{{key}}: {{value}}

sensor:
- name: solar_grandl_math
  unique_id: solar_grandl_math
  icon: mdi:solar-power-variant-outline
  state:  'Attribute für Kalkulation der Solarstrahlung abhängig vom Sonnenstand'

  attributes: 
   {'Albedo': 0.2}

Result:
icon: mdi:solar-power-variant-outline
friendly_name: solar_grandl_math
Albedo: 0.2

Re-usable templates:

re-usable Template / macro is solved.
What is not:
in section attributeS, not attribute!, a template is not rendered.
Gives an error.
Is that solvable?
Thank you!
RT

No*.

You could include a top-level attribute and render a template in that:

attributes:
  root: >
    {{ YOUR TEMPLATE HERE }}

That template could output a dictionary, and you could refer to e.g.:

{{ state_attr('sensor.solar_grandl_math','root')['Albedo'] }}

* It can be done via the API or via MQTT, I think.

OK, i will try it …
Thank you!