Hi,
Ive copied some coding from CTC i350 smart control and successfully controlled my boiler with an Shelly relay based on average price for each day adjusted by helpers.
However, this come with its drawback, when for example the price is generally low an entire day means that there isn’t any warm water for a relaxing evening shower.
So, I need some coding support where automation set by helpers are ignored if the price is below a fixed value.
I would like to use a helper to adjust my limit, but the result should be that any service should be allowed to run if the price is below that threshold.
My Configuration
template:
- sensor:
# The daily reported Price Average is needed in the threshold calculations.
- name: Electricity Price Average
unit_of_measurement: "öre/kWh"
icon: mdi:currency-usd
state: "{{ state_attr('sensor.nordpool_kwh_se3_sek_3_095_025', 'average') | float(0) }}"
# Only used to set up a simple graph.
- name: Electricity Price Min
unit_of_measurement: "öre/kWh"
icon: mdi:currency-usd
state: "{{ state_attr('sensor.nordpool_kwh_se3_sek_3_095_025', 'min') | float(0) }}"
# Only used to set up a simple graph.
- name: Electricity Price Max
unit_of_measurement: "öre/kWh"
icon: mdi:currency-usd
state: "{{ state_attr('sensor.nordpool_kwh_se3_sek_3_095_025', 'max') | float(0) }}"
# Only used to set up a simple graph.
- name: Electricity Price Current
unit_of_measurement: "öre/kWh"
icon: mdi:currency-usd
state: "{{ state_attr('sensor.nordpool_kwh_se3_sek_3_095_025', 'current_price') | float(0) }}"
# Only for info.
- name: Electricity Spotprice from average
unit_of_measurement: "%"
icon: mdi:currency-usd
state: "{{ state_attr('sensor.nordpool_kwh_se3_sek_3_095_025', 'price_percent_to_average') | float(0) }}"
# Calculation from the requested percentage limits and daily Price Average into
# actual "SEK/kWh" limits.
# Please note that the 3 "input_number.threshold..." are "helpers" created from
# Settings -> Devices & Services -> Helpers.
# Then you can create sliders for easy control, by adding the helpers in the
# “Entities Card Configuration”.
- name: Electricity Threshold Limit 1
unit_of_measurement: "öre/kWh"
icon: mdi:currency-usd
state: >
{% set price_avg = state_attr('sensor.nordpool_kwh_se3_sek_3_095_025', 'average') | float(0) %}
{% set threshold = states('input_number.threshold_limit_1_low') | float(0) %}
{{ (price_avg * threshold / 100) | round(2) }}
- name: Electricity Threshold Limit 2
unit_of_measurement: "öre/kWh"
icon: mdi:currency-usd
state: >
{% set price_avg = state_attr('sensor.nordpool_kwh_se3_sek_3_095_025', 'average') | float(0) %}
{% set threshold = states('input_number.threshold_limit_2_medium') | float(0) %}
{{ (price_avg * threshold / 100) | round(2) }}
- name: Electricity Threshold Limit 3
unit_of_measurement: "öre/kWh"
icon: mdi:currency-usd
state: >
{% set price_avg = state_attr('sensor.nordpool_kwh_se3_sek_3_095_025', 'average') | float(0) %}
{% set threshold = states('input_number.threshold_limit_3_high') | float(0) %}
{{ (price_avg * threshold / 100) | round(2) }}
# From the above calculations, choose the appropriate Price Level.
- name: Electricity Price 4 Levels (24h)
unit_of_measurement: ""
icon: mdi:currency-usd
state: >-
{% set price_cur = states('sensor.nordpool_kwh_se3_sek_3_095_025') | float(0) %}
{% set price_avg = state_attr('sensor.nordpool_kwh_se3_sek_3_095_025', 'average') | float(0) %}
{% set threshold_1 = states('input_number.threshold_limit_1_low') | float(0) %}
{% set threshold_2 = states('input_number.threshold_limit_2_medium') | float(0) %}
{% set threshold_3 = states('input_number.threshold_limit_3_high') | float(0) %}
{% if price_cur == 0 or price_avg == 0 %}
-1
{% else %}
{% set price_ratio = (price_cur / price_avg) %}
{% if price_ratio >= (threshold_3 / 100) %}
4
{% elif price_ratio >= (threshold_2 / 100) %}
3
{% elif price_ratio <= (threshold_1 / 100) %}
1
{% else %}
2
{% endif %}
{% endif %}
Automation
alias: IVT control
description: Styra värmepumpen via Nordpool spotpris med relä
trigger:
- platform: state
entity_id:
- sensor.nordpool_kwh_se3_sek_3_095_025
condition: []
action:
- choose:
- conditions:
- condition: numeric_state
entity_id: sensor.electricity_price_4_levels_24h
above: 3
sequence:
- service: light.turn_off
target:
entity_id: light.hue_white_lamp_1
data: {}
- service: switch.turn_on
data: {}
target:
entity_id: switch.shelly1_244cab43e0e5
- service: notify.notify
data:
message: "Nivå 4: dyrt"
title: Expensive
- conditions:
- condition: numeric_state
entity_id: sensor.electricity_price_4_levels_24h
above: 2
sequence:
- service: light.turn_on
target:
entity_id: light.hue_white_lamp_1
data: {}
- service: switch.turn_off
data: {}
target:
entity_id: switch.shelly1_244cab43e0e5
- service: notify.notify
data:
message: "Nivå 3: normal"
title: Normal
- conditions:
- condition: numeric_state
entity_id: sensor.electricity_price_4_levels_24h
above: 1
sequence:
- service: light.turn_on
target:
entity_id: light.hue_white_lamp_1
data: {}
- service: switch.turn_off
data: {}
target:
entity_id: switch.shelly1_244cab43e0e5
- service: notify.notify
data:
message: "Nivå 2: billigt"
title: Cheap
default:
- service: light.turn_on
target:
entity_id: light.hue_white_lamp_1
data: {}
- service: switch.turn_off
data: {}
target:
entity_id: switch.shelly1_244cab43e0e5
- service: notify.notify
data:
message: "Nivå 1: väldigt billigt"
title: Bäst
mode: restart