Any good ideas are welcome. Nordpool Energy Price per hour

Can u please post your full card yaml? i cant get it to work with your Generator code :frowning:

Sorry for the delayed answer. Here comes a more comprehensive answer.

#1 - Add a “Helper”
I use a dropdown heller that looks like this:

#2 - In configuration.yaml add:

sensor:
  - platform: template
    sensors:
      nordpool_eo4_on_hours:
        friendly_name: "Nordpool EO4 Configurable On"
        unit_of_measurement: 'SEK/kWh'
        value_template: >
          {{average((state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'today') | sort(reverse=true))[int(states('input_select.poolpump_avstangd'))-1],
                    (state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'today') | sort(reverse=true))[int(states('input_select.poolpump_avstangd'))]) | 
            round(3) }}
        attribute_templates:
          below_config_value_now: >
            {% if state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'current_price') <= 
                  average((state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'today') | sort(reverse=true))[int(states('input_select.poolpump_avstangd'))-1],
                          (state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'today') | sort(reverse=true))[int(states('input_select.poolpump_avstangd'))]) | 
                  round(3) %}
            True
            {% else %}
            False
            {% endif %}
          current_price: >
            {{ state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'current_price') }}
          today: >
            {{ state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'today') }}
          tomorrow_valid: >
            {{ state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'tomorrow_valid') }}
          tomorrow: >
            {% if state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'tomorrow_valid') %}
            {{average((state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'tomorrow') | sort(reverse=true))[int(states('input_select.poolpump_avstangd'))-1],
                      (state_attr('sensor.nordpool_kwh_se4_sek_3_095_025', 'tomorrow') | sort(reverse=true))[int(states('input_select.poolpump_avstangd'))]) | 
              round(3) }}
            {% else %}
            []
            {% endif %}

#3 - My automation for “cheap” looks like this


Then you add one more for “expensive” where your use “above” instead of “below”.

HTH/Marcus

I once found a code that should be adding a last value duplicate to the array of today prices, but I never managed to make it work. Maybe someone smarter can help us.

Heres the code:

data_generator: |
   var res = entity.attributes.raw_today.map((p, index) => {
      return [new Date(p.start).getTime(), entity.attributes.raw_today[index]["value"]];
   });
   res.push([new Date().getTime(), entity.attributes.raw_today[entity.attributes.raw_today.length() - 1].value];
   return res;

Thanks alot, again!
This was very easy to follow and well explained.
Works like a charm!
Cheers!

Hi.

I am a beginner. I would like to run an electric boiler for 3 hours every day at the cheapest price. But unfortunately I can’t figure it out myself and I haven’t found it either. Maybe someone could help how to do it - who knows, it might take a few minutes :slight_smile:

Check out corvy code her

Is there an option to display prices in cents also?

1 Like

There are a lot of things which should be simple, but are in fact tricky to get perfect. However, it is often not needed to get it perfect. Starting simple and improving where needed is often a good approach.

In your case, I would begin with starting the boiler when the price is at the lowest point of the day and run the boiler for the next 3 hours. This is not guaranteed to be the cheapest, but it’s not far of and relatively easy to implement.

I haven’t tested the code, but it might look something like this:

alias: Hot water
trigger:
  - platform: state
    entity_id:
      - sensor.nordpool
condition:
  - condition: template
    value_template: {{ states('sensor.nordpool') | float == state_attr('sensor.nordpool', 'min') | float }}
action:
  - type: turn_on
      - entity_id: switch.hot_water
  - delay:
      hours: 3
  - type: turn_off
      - entity_id: switch.hot_water

A binary template sensor based on the nordpool sensor:

---
platform: template
sensors:
  cheapest_3_hours:
    value_template: >-
      {% set l=state_attr('sensor.nordpool', '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) }}

The sensor is ON the cheapest 3 hours every day. This can be used for automations etc.

12 Likes

That’s some nice HAbracadabra :heart:

3 Likes

The sensor works very well. Now a new problem. How do I can use it as trigger. No suitable state can be found in the web interface. I canmot. Or you have to write it manually in yaml. I feel very stupid. I have still been able to get other things working without help.

There is no such thing as a stupid question. This is the area where home assistant is not as intuitive it is appears at first glance. I’m barely getting a grasp on this kind of stuff myself.
I think because it is a sensor instead of a binary_sensor, you need to put the state in as a string in the exact right format.

I think this should work:

trigger:
  - platform: state
    entity_id:
      - sensor.energy_low
action:
  - if:
      - condition: state
        entity_id: sensor.energy_low
        state: 'False'
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.boiler
    else:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.boiler

The Test button should tell you if you have done it right
image

1 Like

No need to feel stupid, better to ask and learn!

You can run automations against the entitiy binary_sensor.cheapest_3_hours whenever its on or off.
An example automation:

---
alias: boiler_powersaver
id: boiler_powersaver

trigger:
  - platform: state
    entity_id: binary_sensor.cheapest_3_hours

action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.cheapest_3_hours
            state: "on"
        sequence:
          - service: switch.turn_on
            entity_id: switch.boiler

      - conditions:
          - condition: state
            entity_id: binary_sensor.cheapest_3_hours
            state: "off"
        sequence:
          - service: switch.turn_off
            entity_id: switch.boiler

Built in the UI editor it would be something like this:



1 Like

The link appears to be dead… ?

Now everything works as it should. Thank you for the help.

Don’t remember what that link was about, but currently nordpool integration that I am using is this one: custom-components/nordpool: nordpool sensor for ha. (github.com)
And graph card : RomRider/apexcharts-card: :chart_with_upwards_trend: A Lovelace card to display advanced graphs and charts based on ApexChartsJS for Home Assistant (github.com)

type: custom:apexcharts-card
experimental:
  color_threshold: true
graph_span: 1d
span:
  start: day
series:
  - entity: sensor.nordpool_kwh_lv_eur_2_095_0
    data_generator: |
      return entity.attributes.raw_today.map((entry) => {
        return [new Date(entry.start), entry.value];
      });
    type: column
    float_precision: 2
    show:
      extremas: true
    color_threshold:
      - value: 0
        color: blue
      - value: 0.1
        color: green
      - value: 0.15
        color: darkorange
      - value: 0.2
        color: red
      - value: 0.25
        color: darkred
      - value: 0.3
        color: black
now:
  show: true
  color: '#ff0000'
yaxis:
  - min: 0
    decimals: 2

graph

2 Likes

I have a question: now when the prices are so volatile i have a hard time thinking of how to use the average value over 3days in a sensible way
With spike hours messing up the calculations. I will probably go with the median today value. Any thoughts?

Maybe not suitable for your user case @vilhelm.carlsson but I plan to use sjabby’s logic posted above to turn on the water heater during the cheapest hours and then I modified the same logic from him to get the most expensive hours (i.e the daily peaks) and turn off the house heating (electric radiators) during the peaks.
This is still in the planing phase but I hope to get din rail contactors installed during the fall connected with Shelly’s to control all heating.

2 Likes

I’m a newbie at these stuff with automation
i want to check each hour if is above a certain value lets say 2.2 sek/kWh then the plug is off
but if its under 2.2 sek/kWh plug is on

got the nordpool intergration and have the sensor for current price setup

next would be to have something intergrated with growatt solar, but thats another project later