Stop automation based on fixed value

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

I’ve set up different price levels (very expensive, expensive, normal, cheap, very cheap, zero and negative) based on the nordpool data, and have automations kicking in when the price is “very expensive” and above a threshold (“very expensive” right now is 0.02 kr/kwh, but last winter it was 8.00kr/kwh, so I’ve set the threshold at 2.00), something like that you are looking for?

in template.yaml:

sensor:
  - name: Price electricity
    icon: mdi:currency-usd
    state: >-
      {% set price_cur = states('sensor.nordpool_xxx') | float(0) %}
      {% set price_avg = state_attr('sensor.nordpool_xxx', 'average') | float(0) %}
      {% if price_cur < 0 %}
        Negative
      {% elif price_cur == 0.00 or price_avg == 0 %}
        Free
      {% else %}
        {% set price_ratio = (price_cur / price_avg) %}
        {% if price_ratio >= 1.4 %}
          Very expensive
        {% elif price_ratio >= 1.15 %}
          Expensive
        {% elif price_ratio <= 0.6 %}
          Very cheap
        {% elif price_ratio <= 0.9 %}
          cheap
        {% else %}
          Normal
        {% endif %}
      {% endif %}

the automation:

alias: Turn off heating when very expensive
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.nordpool_xxx
    for:
      hours: 0
      minutes: 1
      seconds: 0
    above: 2
condition:
  - condition: state
    entity_id: sensor.electricity_price
    state: Very expensive
action:
  - service: climate.turn_off
    data: {}
    target:
      entity_id:
        - climate.1
        - climate.2
        - climate.3
  - service: climate.set_temperature
    data:
      entity_id: climate.4
      temperature: "{{ states.climate.4.attributes.temperature - 2 }}"
  - service: notify.mobile_app_xxx
    data:
      title: Heating turned off
      message: >
        Now the power costs {{ states.sensor.nordpool_xxx.state
        }} kr
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: automation.turn_on
    data: {}
    target:
      entity_id: automation.turn_heating_back_on
mode: single

This turns off the floor heating (1, 2 and 3) and lowers the heatpump (4) with two degrees. It finally turns on a seperate automation to start up the floor heating when the price lowers (or the temperature gets uncomfortable)

Hopes this nudges you in the right direction :slight_smile: Could probably use some cleaning up the automation, but it works.

Thanks for the input it helped me to figure out my solution.
In the end it was easy with some explaining by Google bard.

I just needed to add an threshold_0 value linked to an helper and add if criteria before threshold calculations.

     - 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_0 = states('input_number.threshold_limit_0_ignore') | 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 < threshold_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 %}
1 Like