Working script, but can i make it much simpler with a for loop?

Hi

I have a sensor that retrieves the prices for electricity. The data is divided 24 bits containing the price for one hour, and I have used it to create a script that gives me the price for the next 5 hours (and one that list the five cheapest hour etc).

The script works… but I have a hard time imaging that this is the simplest way. And since I am creating a lot of scripts around this sensor I was wondering if someone can give me some tips on how to make it simpler.

The script looks like this:

alias: Elpriser_5_timer
sequence:
  - service: tts.google_translate_say
    data:
      entity_id: media_player.bogreol
      message: |-
        {% set elpris = states("sensor.elpriser_uge_mean") %}
         {% set list = state_attr("sensor.energi_data_service", "raw_today") %}{% set
          list_forecast = state_attr("sensor.energi_data_service", "forecast") %}{%
              set dict00 = list_forecast[0]  %}{% set dict01 = list_forecast[1]  %} {% set
              dict02 = list_forecast[2]  %} {% set dict03 = list_forecast[3]  %} {% set
              dict04 = list_forecast[4]  %} {% set dict05 = list_forecast[5]  %} {% set
              dict06 = list_forecast[6]  %} {% set dict07 = list_forecast[7]  %} {% set
              dict08 = list[8]  %} {% set dict09 = list[9]  %} {% set dict10 = list[10]  %}
              {% set dict11 = list[11]  %} {% set dict12 = list[12]  %} {% set dict13 =
              list[13]  %} {% set dict14 = list[14]  %} {% set dict15 = list[15]  %} {%
              set dict16 = list[16]  %} {% set dict17 = list[17]  %} {% set dict18 =
              list[18]  %} {% set dict19 = list[19]  %} {% set dict20 = list[20]  %} {%
              set dict21 = list[21]  %} {% set dict22 = list[22]  %} {% set dict23 =
              list[23]  %}

              {% set full_list = {"00":dict00.price,
                                  "01":dict01.price, 
                                  "02":dict02.price,
                                  "03":dict03.price,
                                  "04":dict04.price,
                                  "05":dict05.price,
                                  "06":dict06.price,
                                  "07":dict07.price,
                                  "08":dict08.price,
                                  "09":dict09.price,
                                  "10":dict10.price,
                                  "11":dict11.price,
                                  "12":dict12.price,
                                  "13":dict13.price,
                                  "14":dict14.price,
                                  "15":dict15.price,
                                  "16":dict16.price,
                                  "17":dict17.price,
                                  "18":dict18.price,
                                  "19":dict19.price,
                                  "20":dict20.price,
                                  "21":dict21.price,
                                  "22":dict22.price,
                                  "23":dict23.price,
                                  "24":dict00.price,
                                  "25":dict01.price,
                                  "26":dict02.price,
                                  "27":dict03.price,
                                  "28":dict04.price} 
                                          %}

                                        {%set tidspunkt01 = now().hour %}
                                        {%set tidspunkt02 = now().hour + 1 %}
                                        {%set tidspunkt03 = now().hour + 2 %}
                                        {%set tidspunkt04 = now().hour + 3 %}
                                        {%set tidspunkt05 = now().hour + 4 %}

                                        {% set time01 = tidspunkt01 | string %}
                                        {% set time02 = tidspunkt02 | string %}
                                        {% set time03 = tidspunkt03 | string %}
                                        {% set time04 = tidspunkt04 | string %}
                                        {% set time05 = tidspunkt05 | string %}


                                          {%set hour01 = full_list[time01] | string %}
                                          {%set hour02 = full_list[time02] | string %}
                                          {%set hour03 = full_list[time03] | string %}
                                          {%set hour04 = full_list[time04] | string %}
                                          {%set hour05 = full_list[time05] | string %}

          Prisen lige nu er {{ hour01.0 }} komma {{ hour01.2 }} {{ hour01.3 }} kroner per KILOWATT. 
          Prisen klokken {{ tidspunkt02 }} er {{ hour02.0 }} komma {{ hour02.2 }} {{ hour02.3 }} kroner per Kilowatt 
          Prisen klokken {{ tidspunkt03 }} er {{ hour03.0 }} komma {{ hour03.2 }} {{ hour03.3 }} kroner per Kilowatt 
          Prisen klokken {{ tidspunkt04 }} er {{ hour04.0 }} komma {{ hour04.2 }} {{ hour04.3 }} kroner per Kilowatt  
          Prisen klokken {{ tidspunkt05 }} er {{ hour05.0 }} komma {{ hour05.2 }} {{ hour05.3 }} kroner per Kilowatt 
          Gennemsnitsprisen for de sidste 7 dage er {{elpris.0}} komma {{elpris.2}} {{elpris.3}} {{elpris.4}} 
kroner per kilowatt
mode: single

The data from state_attr(“sensor.energi_data_service”, “raw_today”) looks like this (and forecast is identically but 24 hours later so I use the data from forecast for the hours between 00 and 07.00)

[{'hour': datetime.datetime(2022, 12, 7, 0, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.461}, {'hour': datetime.datetime(2022, 12, 7, 1, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.192}, {'hour': datetime.datetime(2022, 12, 7, 2, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 1.897}, {'hour': datetime.datetime(2022, 12, 7, 3, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 1.86}, {'hour': datetime.datetime(2022, 12, 7, 4, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.141}, {'hour': datetime.datetime(2022, 12, 7, 5, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.413}, {'hour': datetime.datetime(2022, 12, 7, 6, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.622}, {'hour': datetime.datetime(2022, 12, 7, 7, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.534}, {'hour': datetime.datetime(2022, 12, 7, 8, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.768}, {'hour': datetime.datetime(2022, 12, 7, 9, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.667}, {'hour': datetime.datetime(2022, 12, 7, 10, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.52}, {'hour': datetime.datetime(2022, 12, 7, 11, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.307}, {'hour': datetime.datetime(2022, 12, 7, 12, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.534}, {'hour': datetime.datetime(2022, 12, 7, 13, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.198}, {'hour': datetime.datetime(2022, 12, 7, 14, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.795}, {'hour': datetime.datetime(2022, 12, 7, 15, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 4.062}, {'hour': datetime.datetime(2022, 12, 7, 16, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 4.223}, {'hour': datetime.datetime(2022, 12, 7, 17, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.9}, {'hour': datetime.datetime(2022, 12, 7, 18, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.72}, {'hour': datetime.datetime(2022, 12, 7, 19, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.612}, {'hour': datetime.datetime(2022, 12, 7, 20, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.802}, {'hour': datetime.datetime(2022, 12, 7, 21, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.043}, {'hour': datetime.datetime(2022, 12, 7, 22, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.73}, {'hour': datetime.datetime(2022, 12, 7, 23, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.534}]

I am not the sharpest tool, when it comes to Python, but have the feeling that I can do this a lot smarter with some kind of for loop. But never got further than this which dont work.

{% set list = state_attr("sensor.energi_data_service", "raw_today") %}
{% for element in list %}
  {% set new_list = element %}
  
{% endfor %}
{{full_list}}

I can print the result if i I change {% set new_list = element %} to {{element}} but for some reason I cant turn it into a list.

{% set list = state_attr("sensor.energi_data_service", "raw_today") %}
{% for element in list %}
  {{element }}
  
{% endfor %}

But the result is a bit simpler to work with than the result without the for loop…

{'hour': datetime.datetime(2022, 12, 7, 0, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.461}
  

  {'hour': datetime.datetime(2022, 12, 7, 1, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.192}
  

  {'hour': datetime.datetime(2022, 12, 7, 2, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 1.897}
  

  {'hour': datetime.datetime(2022, 12, 7, 3, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 1.86}
  

  {'hour': datetime.datetime(2022, 12, 7, 4, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.141}
  

  {'hour': datetime.datetime(2022, 12, 7, 5, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.413}
  

  {'hour': datetime.datetime(2022, 12, 7, 6, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.622}
  

  {'hour': datetime.datetime(2022, 12, 7, 7, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.534}
  

  {'hour': datetime.datetime(2022, 12, 7, 8, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.768}
  

  {'hour': datetime.datetime(2022, 12, 7, 9, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.667}
  

  {'hour': datetime.datetime(2022, 12, 7, 10, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.52}
  

  {'hour': datetime.datetime(2022, 12, 7, 11, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.307}
  

  {'hour': datetime.datetime(2022, 12, 7, 12, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.534}
  

  {'hour': datetime.datetime(2022, 12, 7, 13, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.198}
  

  {'hour': datetime.datetime(2022, 12, 7, 14, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.795}
  

  {'hour': datetime.datetime(2022, 12, 7, 15, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 4.062}
  

  {'hour': datetime.datetime(2022, 12, 7, 16, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 4.223}
  

  {'hour': datetime.datetime(2022, 12, 7, 17, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.9}
  

  {'hour': datetime.datetime(2022, 12, 7, 18, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.72}
  

  {'hour': datetime.datetime(2022, 12, 7, 19, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.612}
  

  {'hour': datetime.datetime(2022, 12, 7, 20, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.802}
  

  {'hour': datetime.datetime(2022, 12, 7, 21, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 3.043}
  

  {'hour': datetime.datetime(2022, 12, 7, 22, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.73}
  

  {'hour': datetime.datetime(2022, 12, 7, 23, 0, tzinfo=<DstTzInfo 'Europe/Copenhagen' CET+1:00:00 STD>), 'price': 2.534}

any help will be appreciated