How to automate AC on average electricity spot price

I’mm trying control my AC in simple way so, that when ever electricity price goes above today’s average electricity price, the AC will be set 1 degree lower. When ever the electricity price goes lower than average price for the day, temperature will set one degree higher.

I have automation entries which work when I set above: 8 and below 8. But when I try to use: above: “{{ float(state_attr(‘sensor.nordpool_kwh_fi_eur_3_10_024’, ‘average’)) }}” I get error message: “Automation with alias ‘If spot price above 8 cents set heat 1C lower’ failed to setup triggers and has been disabled: expected float for dictionary value @ data[‘above’]. Got None”

I have tried with without float and use different syntax but I always get same error.

My entries (which work otherwise when I set above:8 | below: 8):

# If SPOT price is lowish we can increase heating by 1C during day time
- id: hallway_ac_fan_low_spot
  alias: If spot price below 7 cents increase heat
  description: 'With low price increase heat by 1'
  trigger:
  - platform: numeric_state
    entity_id: sensor.nordpool_kwh_fi_eur_3_10_024
    below: "{{ state_attr('sensor.nordpool_kwh_fi_eur_3_10_024', 'average') }}"                                                                                         
  - platform: numeric_state
    entity_id: sensor.ac_12488762_outdoor_temperature
    below: 2
  condition: 
    condition: and 
    conditions:
      - condition: time
        after: '10:00'
        before: '17:00'
  action:
  - service: climate.set_temperature
    data:
      temperature: "{{ state_attr('climate.ac_12494102', 'temperature') + 1 }}"  # Increase temperature by 1 degree
    target:
      entity_id: climate.ac_12494102
  mode: single

# If SPOT price is above average we can decrease heating by 1C during day time                                                                                         
- id: hallway_ac_fan_expensive_spot                 
  alias: If spot price above 7 cents set heat 1C lower
  description: ''                                   
  trigger:                                          
  - platform: numeric_state                         
    entity_id: sensor.nordpool_kwh_fi_eur_3_10_024
    above: "{{ state_attr('sensor.nordpool_kwh_fi_eur_3_10_024', 'average') }}"
  condition:                                        
    condition: and                                  
    conditions:                                     
      - condition: time                             
        after: '10:00'                              
        before: '17:00'                             
  action:                                           
  - service: climate.set_temperature                
    data:                                           
      temperature: "{{ state_attr('climate.ac_12494102', 'temperature') - 1 }}"  # Decrease temperature by 1 degree
    target:                                         
      entity_id: climate.ac_12494102                
  mode: single   


Any help would be greatly appreciated.

Thank you,

-Tapio

I’m answering to myself the solution. I created a following sensor:

    energy_spot_average_price:
      friendly_name: "Nordpool Average Spot Price"
      unit_of_measurement: 'c/kWh'
      value_template: "{{ state_attr('sensor.nordpool_kwh_fi_eur_3_10_024', 'average') | float | round(2) }}"

Then I used that on my config:

# If SPOT price is below average we can increase heating by 1C during day time
- id: hallway_ac_fan_low_spot
  alias: If spot price below average increase heat
  description: 'With low price increase heat by 1'
  trigger:
  - platform: numeric_state
    entity_id: sensor.nordpool_kwh_fi_eur_3_10_024
    below: sensor.energy_spot_average_price
  - platform: numeric_state
    entity_id: sensor.ac_12488762_outdoor_temperature
    below: 2
  condition: 
    condition: and 
    conditions:
      - condition: time
        after: '08:00'
        before: '17:00'
  action:
  - service: climate.set_temperature
    data:
      temperature: "{{ state_attr('climate.ac_12494102', 'temperature') + 1 }}"  # Increase temperature by 1 degree
    target:
      entity_id: climate.ac_12494102
  mode: single

# If SPOT price is above average let's set heating 1C lower
- id: hallway_ac_fan_expensive_spot
  alias: If spot price above average cents set heat 1C lower
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.nordpool_kwh_fi_eur_3_10_024
    above: sensor.energy_spot_average_price
  condition: 
    condition: and 
    conditions:
      - condition: time
        after: '08:00'
        before: '22:00'
  action:
  - service: climate.set_temperature
    data:
      temperature: "{{ state_attr('climate.ac_12494102', 'temperature') - 1 }}"  # Decrease temperature by 1 degree
    target:
      entity_id: climate.ac_12494102
  mode: single

That seems to work the way I wanted although I need to wait a bit longer. When setting the value for sensor.nordpool_kwh_fi_eur_3_10_024 manually through “Developer Tools” it does change the temperature the way I was expecting.

Real results I should get during the day, when the price drops below average and the back above average within next few hours.

Thank you,

-Tapio

This may not work exactly as you expect:

If the average changes, the trigger won’t fire even if that would cause a threshold to be crossed.

However, if it’s a daily average and the spot price is moving often, it’ll probably work fine.

Hourly spot prices for the next day are announced day before around 1pm CET. They don’t change after that so the average price stays constant throughout the day. The hourly price obviously changes every hour but follows the prices which were published day before.

In theory this should work and so it seems, but I need to see the changing in real life first, to be able to fully qualify.

For today it looks like this:
image

Average is 7.95c/kWh and current 8.55c/kWh. It will go below 7.95c/kWh at 12pm CET.

Thanks,

-Tapio

1 Like

Replying to myself gain noting that the solution I implemented works as I expected and the temperature request wen fro 20C to 21C when sport price went less than the average:
image
image

Now I need to wait it works the other way around as well :slight_smile:

Thanks,

-Tapio

Ok, now I have verified everything works for both increasing the temperature when prices exceeds the average and decreasing the temperature when price goes under the average.

My setup and the way I set everything up is described here: Home Assistant: Heat Pump Automation with Cheap SPOT hours and Github Copilot doing the work | Auroran Runner

Thanks,

-Tapio

If the outside temperature fluctuates around 2°C your set temperature will rise every time the temperature crosses below 2°C. Left to its own devices, this pair of automations will increase the set temperature without bounds. You should probably set a maximum and minimum in the conditions.

You are correct. The outside temperature should be on conditions and not on triggers.

I want the spot price to trigger temperature raise only when it’s less than 2C outside.

What would be correct syntax on conditions then?

Thanks,

-Tapio

Explain the exact logic you want to use, because you also have time conditions in there.

Imagine you’re doing the job of the automation, and you can see the outside temperature, the current set temperature, and the electricity spot and average prices. When will you adjust the temperature in each direction?

So, I want the heat increase only to happen between the time constraints and when it’s less than 2 degrees outside.

Heat decrease I’m ok between time constraints but never more often than once after heat increase or once without heat increase. It would be good to have minimum there though as condition. For example 18C.

That would be really awesome :slight_smile:

Thanks,

-Tapio

This could be tricky: what would trigger consecutive heat changes?

The totally untested version below should set the temperature to 21 if it’s cold, cheap and daytime; and 19 if any of those is no longer true. Will trigger off any change to any of the related entities.

trigger:
  - platform: state
    entity_id:
      - sensor.nordpool_kwh_fi_eur_3_10_024
      - sensor.energy_spot_average_price
      - sensor.ac_12488762_outdoor_temperature
  - platform: time
    at:
      - "08:00"
      - "22:00"
condition:
  - or:
    - and:
      - alias: Cold and cheap and daytime so temp up
      - condition: numeric_state
        entity_id: sensor.ac_12488762_outdoor_temperature
        below: 2
      - condition: numeric_state
        entity_id: sensor.nordpool_kwh_fi_eur_3_10_024
        below: sensor.energy_spot_average_price
      - condition: time
        after: "08:00"
        before: "22:00"
    - or:
      - alias: Warm or expensive or night so temp down
      - condition: numeric_state
        entity_id: sensor.ac_12488762_outdoor_temperature
        above: 2
      - condition: numeric_state
        entity_id: sensor.nordpool_kwh_fi_eur_3_10_024
        above: sensor.energy_spot_average_price
      - condition: time
        before: "08:00"
      - condition: time
        after: "22:00"
action:
  - variables:
      down: >
        {{ states('sensor.ac_12488762_outdoor_temperature')|float(0) > 2 or
           states('sensor.nordpool_kwh_fi_eur_3_10_024')|float(0) > 
             states('sensor.energy_spot_average_price')|float(0) or
           not(8 <= now().hour < 22) }}
  - service: climate.set_temperature
    data:
      temperature: "{{ 19 if down else 21 }}
    target:
      entity_id: climate.ac_12494102

This is great :slight_smile: Thank you!

I will test this with some time :+1: