How to write sensor output template for attributes that are time series (like Nordpool electricity prices)

Hello,

How to write sensor output template so that it looks like “raw_today” in the screenshot?

Those are the electricity prices provided by the Nordpool sensor. My intention is to read those prices, perform a few calculations, and then output similar object that is provided by the Nordpool sensors. I have managed to read the values and perform computations based on them using the code below.

{% set today = state_attr("sensor.nordpool_kwh_fi_eur_3_095_024","raw_today")%}

{% set uusi=namespace(value=[]) %}

{% for i in range(3) %}

{% set x = today[i] %}

{% if x.value < 5 %}

{% set y=100 %}

{% else %}

{% set y=0 %}

{% endif %}

{% set x = dict(x,value=y) %}

{% set uusi.value=uusi.value+ [x] %}

{% endfor%}

{{ uusi.value }}

[{‘start’: datetime.datetime(2022, 12, 29, 0, 0, tzinfo=zoneinfo.ZoneInfo(key=‘Europe/Helsinki’)), ‘end’: datetime.datetime(2022, 12, 29, 1, 0, tzinfo=zoneinfo.ZoneInfo(key=‘Europe/Helsinki’)), ‘value’: 0}, {‘start’: datetime.datetime(2022, 12, 29, 1, 0, tzinfo=zoneinfo.ZoneInfo(key=‘Europe/Helsinki’)), ‘end’: datetime.datetime(2022, 12, 29, 2, 0, tzinfo=zoneinfo.ZoneInfo(key=‘Europe/Helsinki’)), ‘value’: 0}, {‘start’: datetime.datetime(2022, 12, 29, 2, 0, tzinfo=zoneinfo.ZoneInfo(key=‘Europe/Helsinki’)), ‘end’: datetime.datetime(2022, 12, 29, 3, 0, tzinfo=zoneinfo.ZoneInfo(key=‘Europe/Helsinki’)), ‘value’: 0}]

The type of output is a string. How to write the attribute_templates -section in the sensor specification so that the daily start, end, and value fields are accessible like in the Nordpool sensor?

After coming up with good keywords I think I managed to find an answer: for the general case my code works as expected. However, for the particular use with the Nordpool sensor this is undoable: datetime-objects are not supported as outputs and the whole dictionary becomes just a string. The problem is discussed here:
Attributes in Template senor can only be simple objects (int, float, bool, string) if they are inside dict or list - #8 by petro

If I replace the datetime-objects like this:

{% set x = dict(x,value=y, start=1, end=2) %}

then the output structure works correctly and I can access all the attribute values of the sensor. I guess the workaround in my specific problem is to represent the datetime-information with simpler objects.