I have this in my config to accommodate this need:
binary_sensor:
- platform: template
sensors:
nordpool_cheapest_4_hours:
value_template: >-
{% set l=state_attr('sensor.nordpool_kwh_oslo_nok_2_10_025', 'raw_today')|sort(attribute='value') %}
{{ (now() >= l[0].start and now() <= l[0].end)
or (now() >= l[1].start and now() <= l[1].end)
or (now() >= l[2].start and now() <= l[2].end)
or (now() >= l[3].start and now() <= l[3].end) }}
nordpool_cheapest_8_hours:
value_template: >-
{% set l=state_attr('sensor.nordpool_kwh_oslo_nok_2_10_025', 'raw_today')|sort(attribute='value') %}
{{ (now() >= l[0].start and now() <= l[0].end)
or (now() >= l[1].start and now() <= l[1].end)
or (now() >= l[2].start and now() <= l[2].end)
or (now() >= l[3].start and now() <= l[3].end)
or (now() >= l[4].start and now() <= l[4].end)
or (now() >= l[5].start and now() <= l[5].end)
or (now() >= l[6].start and now() <= l[6].end)
or (now() >= l[7].start and now() <= l[7].end) }}
nordpool_cheapest_12_hours:
value_template: >-
{% set l=state_attr('sensor.nordpool_kwh_oslo_nok_2_10_025', 'raw_today')|sort(attribute='value') %}
{{ (now() >= l[0].start and now() <= l[0].end)
or (now() >= l[1].start and now() <= l[1].end)
or (now() >= l[2].start and now() <= l[2].end)
or (now() >= l[3].start and now() <= l[3].end)
or (now() >= l[4].start and now() <= l[4].end)
or (now() >= l[5].start and now() <= l[5].end)
or (now() >= l[6].start and now() <= l[6].end)
or (now() >= l[7].start and now() <= l[7].end)
or (now() >= l[8].start and now() <= l[8].end)
or (now() >= l[9].start and now() <= l[9].end)
or (now() >= l[10].start and now() <= l[10].end)
or (now() >= l[11].start and now() <= l[11].end) }}
This gives me a binary_sensor for the hours I am looking for. For instance I automate some of the heating in the house based on the 12 cheapest hours. Hope this helps!
Note, you need to install the nordpool custom component: GitHub - custom-components/nordpool: nordpool sensor for ha.