Change the "value" of sensor output based on calculation

Any help how to create a custom template sensor (or something else) that would convert “value” from certain sensor to something else if it fits given requirements.
My end goal is to be able to split hourly energy price’s into 5 groups based on price and show how they will spread around the day in apex chart.

I’m using Nordpool integration as a source sensor. It contains this information for every hour and they are released +12 prior the start of the block.

{'start': datetime.datetime(2023, 1, 2, 11, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Helsinki')), 'end': datetime.datetime(2023, 1, 2, 12, 0, tzinfo=zoneinfo.ZoneInfo(key='Europe/Helsinki')), 'value': 16.317}

I can additionally extract all the values from it with ie.

{{ state_attr('sensor.nordpool_kwh_fi_eur_3_09_01', 'today')}}

[4.15, 6.67, 5.984, 6.115, 5.158, 5.809, 8.08, 11.582, 15.501, 16.358, 16.476, 16.317, 16.069, 16.182, 16.114, 16.602, 17.387, 18.218, 19.0, 19.521, 18.391, 17.13, 15.884, 14.784]

What I wish to happen is to create new sensor that outputs all the same timestamps but instead of price in cents for value I’m looking for number 1-5 based on price level. (I already have a “if this then that group - elif that” type of rule written so it is taken care)

So how to copy the sensor data to passthrough with new value for “value”… :man_shrugging:

Notes:

{{ state_attr('sensor.nordpool_kwh_fi_eur_3_09_01', 'today') |float }}
Is not feasible “ValueError: Template error: float got invalid input”

But specific hour is.
{{ state_attr('sensor.nordpool_kwh_fi_eur_3_09_01', 'today')[11]|float }}

So I can run a template sensor for every hour from 1-24 and create new output values based on price compared to average but I am not able to maintain that timestamp and I dont think that it’s the way to do it (having 24 templates for one “simple” issue)

I’m assuming you want a sensor that at the start of the day has a value of 4.15, then an hour later changes to 6.67 etc (using the example data you provided). You could create a template sensor that triggers based on time pattern to run every hour, and then pull out the relevant data for the hour.

For example, using Templates under Dev Tools:

{% set values = [4.15, 6.67, 5.984, 6.115, 5.158, 5.809, 8.08, 11.582, 15.501, 16.358, 16.476, 16.317, 16.069, 16.182, 16.114, 16.602, 17.387, 18.218, 19.0, 19.521, 18.391, 17.13, 15.884, 14.784] %}
{% set hour = now().strftime('%H') | int %}
{{ values[hour] }}

Obviously you would pick up the data from your sensor, rather than having the values hard-coded. Is that what you’re after?