Run during cheapest hour afternoon

this is how I run my bbqkees controlled warmwater boiler during cheap hours in the afternoon (dynamic electricity contract).
Most of it is copy pasted from elsewhere which I can’t find anymore.
First have the nordpool integration up and running.
Then create this sensor:
(in sensor.yaml , I use file editor)

  - platform: template
    sensors:
      cheapest_hours_energy_tomorrow:
        device_class: timestamp
        friendly_name: Cheapest Mittag sequential electricity hours
        value_template: >
          {%- set numberOfSequentialHours = 1 -%}
          {%- set lastHour = 17 -%}
          {%- set firstHour = 11 -%}

          {%- if state_attr('sensor.nordpool_kwh_nl_eur_3_09_0', 'tomorrow_valid') == true -%}
            {%- set ns = namespace(counter=0, list=[], cheapestHour=today_at("00:00") + timedelta( hours = (24)), cheapestPrice=999.00) -%}
            {%- for i in range(firstHour + numberOfSequentialHours, lastHour+1) -%}
              {%- set ns.counter = 0.0 -%}
              {%- for j in range(i-numberOfSequentialHours, i) -%}
                {%- set ns.counter = ns.counter + state_attr('sensor.nordpool_kwh_nl_eur_3_09_0', 'tomorrow')[j] -%}
              {%- endfor -%}
              {%- set ns.list = ns.list + [ns.counter] -%}
              {%- if ns.counter < ns.cheapestPrice -%}
                {%- set ns.cheapestPrice = ns.counter -%}
                {%- set ns.cheapestHour = today_at("00:00") + timedelta( hours = (24 + i - numberOfSequentialHours)) -%}
              {%- endif -%}
            {%- endfor -%}
            {{ ns.cheapestHour }}
            {%- set ns.cheapestPrice = ns.cheapestPrice / numberOfSequentialHours -%}
          {%- endif -%}

create a helper like this:
input_datetime.device_start_time
and an automation:

alias: Set dhw start time for next afternoon at 6pm
description: ""
trigger:
  - platform: time
    at: "18:00:00"
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      time: >-
        {{ (as_timestamp(states('sensor.cheapest_hours_energy_tomorrow')) +
        (3600*0.2))| timestamp_custom('%H:%M') }}
    target:
      entity_id: input_datetime.device_start_time
mode: single

and finally this automation:

alias: dhw on device start time
description: ""
trigger:
  - platform: time
    at: input_datetime.device_start_time
condition:
  - condition: numeric_state
    entity_id: sensor.boiler_wwcurtemp2
    below: "33"
    enabled: true
action:
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - device_id: f
    domain: select
    entity_id: select.thermostat_wwmode
    type: select_option
    option: normal
  - delay:
      hours: 0
      minutes: 5
      seconds: 20
      milliseconds: 0
  - device_id: f
    domain: select
    entity_id: select.thermostat_wwmode
    type: select_option
    option: "off"
mode: single

here my heating automation for the x cheapest hours of the day:

alias: heat when x cheapest hours
description: ""
triggers:
  - hours: "*"
    minutes: /15
    seconds: "11"
    trigger: time_pattern
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.cheapest_x_hours
            state: "off"
        sequence:
          - device_id: xyz
            domain: number
            entity_id: number.thermostat_hc1_manualtemp
            type: set_value
            value: 8
          - action: select.select_option
            metadata: {}
            data:
              option: auto
            target:
              entity_id: select.boiler_hppumpmode
            enabled: true
          - choose:
              - conditions:
                  - condition: state
                    entity_id: sensor.boiler_hpactivity
                    state: heating
                    for:
                      hours: 0
                      minutes: 10
                      seconds: 0
                sequence:
                  - action: number.set_value
                    metadata: {}
                    data:
                      value: "50"
                    target:
                      entity_id: number.boiler_hphystheat
            default:
              - delay:
                  hours: 0
                  minutes: 5
                  seconds: 0
                  milliseconds: 0
              - action: number.set_value
                metadata: {}
                data:
                  value: "50"
                target:
                  entity_id: number.boiler_hphystheat
          - condition: numeric_state
            entity_id: sensor.nordpool_diff_triangle_2
            below: -2
            enabled: true
          - wait_for_trigger:
              - trigger: time_pattern
                minutes: "13"
              - trigger: time_pattern
                minutes: "27"
              - trigger: time_pattern
                minutes: "44"
              - trigger: time_pattern
                minutes: "57"
            enabled: true
          - action: select.select_option
            metadata: {}
            data:
              option: continuous
            target:
              entity_id: select.boiler_hppumpmode
            enabled: true
    default:
      - delay:
          hours: 0
          minutes: 0
          seconds: 28
          milliseconds: 0
      - choose:
          - conditions:
              - condition: numeric_state
                entity_id: sensor.nordpool_combined_quarterly
                below: -5
            sequence:
              - device_id: xyz334
                domain: number
                entity_id: number.thermostat_hc1_manualtemp
                type: set_value
                value: 8
              - action: select.select_option
                metadata: {}
                data:
                  option: auto
                target:
                  entity_id: select.boiler_hppumpmode
                enabled: true
              - choose:
                  - conditions:
                      - condition: state
                        entity_id: sensor.boiler_hpactivity
                        state: heating
                        for:
                          hours: 0
                          minutes: 10
                          seconds: 0
                    sequence:
                      - action: number.set_value
                        metadata: {}
                        data:
                          value: "50"
                        target:
                          entity_id: number.boiler_hphystheat
                default:
                  - delay:
                      hours: 0
                      minutes: 5
                      seconds: 0
                      milliseconds: 0
                  - action: number.set_value
                    metadata: {}
                    data:
                      value: "50"
                    target:
                      entity_id: number.boiler_hphystheat
              - condition: numeric_state
                entity_id: sensor.nordpool_diff_triangle_2
                below: -2
                enabled: true
              - wait_for_trigger:
                  - trigger: time_pattern
                    minutes: "13"
                  - trigger: time_pattern
                    minutes: "27"
                  - trigger: time_pattern
                    minutes: "44"
                  - trigger: time_pattern
                    minutes: "57"
                enabled: true
              - action: select.select_option
                metadata: {}
                data:
                  option: continuous
                target:
                  entity_id: select.boiler_hppumpmode
                enabled: true
        default:
          - action: select.select_option
            metadata: {}
            data:
              option: continuous
            target:
              entity_id: select.boiler_hppumpmode
          - choose:
              - conditions:
                  - condition: numeric_state
                    entity_id: number.thermostat_hc1_manualtemp
                    above: 15
                sequence:
                  - action: number.set_value
                    metadata: {}
                    data:
                      value: "150"
                    target:
                      entity_id: number.boiler_hphystheat
                  - device_id: 123rr
                    domain: number
                    entity_id: number.thermostat_hc1_manualtemp
                    type: set_value
                    value: 19.5
              - conditions:
                  - condition: numeric_state
                    entity_id: number.thermostat_hc1_manualtemp
                    below: 15
                sequence:
                  - device_id: xyz
                    domain: number
                    entity_id: number.thermostat_hc1_manualtemp
                    type: set_value
                    value: 30
                  - wait_for_trigger:
                      - trigger: state
                        entity_id:
                          - sensor.boiler_hpactivity
                    timeout:
                      hours: 0
                      minutes: 14
                      seconds: 0
                      milliseconds: 0
                  - device_id: xyz
                    domain: number
                    entity_id: number.thermostat_hc1_manualtemp
                    type: set_value
                    value: 19.5
                  - action: number.set_value
                    metadata: {}
                    data:
                      value: "150"
                    target:
                      entity_id: number.boiler_hphystheat
mode: single