Tibber price level to turn on heating?

Right now it looks like this in HA dashboard:


And as you can see the “History graph card configuration” seems to interpret my numbers as text…?
Please help me out!

Also, please explain the use of “combined” :confused:

I’ve included the following to my configuration.yaml:

template:
  - sensor:
      - name: Electricity Price - 5 Levels (24h)
        icon: mdi:currency-usd
        state: >-
          {% set price_cur = states('sensor.electricity_price_majbosv6') | float(0) %}
          {% set price_avg = state_attr('sensor.electricity_price_majbosv6', 'avg_price') | float(0) %}
          {% if price_cur == 0 or price_avg == 0 %}
            unknown
          {% else %}
            {% set price_ratio = (price_cur / price_avg) %}
            {% if price_ratio >= 1.4 %}
              5
            {% elif price_ratio >= 1.15 %}
              4
            {% elif price_ratio <= 0.6 %}
              1
            {% elif price_ratio <= 0.9 %}
              2
            {% else %}
              3
            {% endif %}
          {% endif %}
      - name: Electricity Price - 5 Levels (Tibber 3-days)
        icon: mdi:currency-usd
        state: >-
          {% set enum_lvl = state_attr('sensor.electricity_price_majbosv6', 'price_level') %}
          {% if enum_lvl == "VERY_CHEAP" %}
            1
          {% elif enum_lvl == "CHEAP" %}
            2
          {% elif enum_lvl == "NORMAL" %}
            3
          {% elif enum_lvl == "EXPENSIVE" %}
            4
          {% else %} # enum_lvl == "VERY_EXPENSIVE"
            5
          {% endif %}
      - name: Electricity Price - 5 Levels (combined)
        icon: mdi:currency-usd
        state: >-
          {% set level1 = states('sensor.electricity_price_5_levels_24h') %}
          {% set level3 = states('sensor.electricity_price_5_levels_tibber_3_days') %}
          {% if level1 == level3 %}
            {{ level1 }}
          {% elif level1 in ['unknown','undefined','none'] or level3 in ['unknown','undefined','none'] %}
            unknown
          {% elif level1 == "1" %}
            {{ level3 }}
          {% elif level3 == "1" %}
            {{ level1 }}
          {% elif level1 == "2" %}
            {{ level3 }}
          {% elif level3 == "2" %}
            {{ level1 }}
          {% elif level1 == "3" %}
            {{ level3 }}
          {% elif level3 == "3" %}
            {{ level1 }}
          {% elif level1 == "4" %}
            {{ level3 }}
          {% else %}
            {{ level1 }}
          {% endif %}

I will try… :smiley:

The original Tibber API have this “Price Level” comparing the current value with the average value in the past 3 days. I like that, but for some devices I’d like the same levels, but compared only with the current day, so you will find the 2 sensors with “(1-day)” or “(3-days)” in the name.
The “Combined” will result the worse case between those 2 sensors. Let’s say the price now is “VERY_CHEAP” in the 3-days sensor, but it’s “NORMAL” in the 1-day (this will happen in a day where electricity is cheaper than the previous 2 days). The result of the combined sensor will be “NORMAL”, as this is the worse case between those two.
In the same way, it can be that today is way more expensive than the previous days, so it can happy that at certain time, the 1-day sensor will result in “CHEAP”, but when comparing to the 3-days, that would be “EXPENSIVE”, to the combine will return “EXPENSIVE” (the worse between the 2 other sensors). It will be rarer to have “VERY_CHEAP” in the combined sensor.

I have some devices where I don’t have to turn on every day… let’s say, I have a bench with lots of battery chargers where my electric screwdriver, a AAA-battery charger, my camera’s charger, etc. are all connected to a switch. I wanna those devices to be charging only when the price is really cheap, so I use the combined sensor to find when it was “VERY_CHEAP” on both the 1-day and the 3-day sensors.

Did I succeeded in explaining this? :smiley:

Yes, I believe to have numbers as results you should have something like this (it’s ugly, maybe we should hve the values into a variable and then convert to int later):

template:
  - sensor:
      - name: Electricity Price - 5 Levels (24h)
        icon: mdi:currency-usd
        state: >-
          {% set price_cur = states('sensor.electricity_price_majbosv6') | float(0) %}
          {% set price_avg = state_attr('sensor.electricity_price_majbosv6', 'avg_price') | float(0) %}
          {% if price_cur == 0 or price_avg == 0 %}
            -1 | int(0)
          {% else %}
            {% set price_ratio = (price_cur / price_avg) %}
            {% if price_ratio >= 1.4 %}
              5 | int(0)
            {% elif price_ratio >= 1.15 %}
              4 | int(0)
            {% elif price_ratio <= 0.6 %}
              1 | int(0)
            {% elif price_ratio <= 0.9 %}
              2 | int(0)
            {% else %}
              3 | int(0)
            {% endif %}
          {% endif %}
      - name: Electricity Price - 5 Levels (Tibber 3-days)
        icon: mdi:currency-usd
        state: >-
          {% set enum_lvl = state_attr('sensor.electricity_price_majbosv6', 'price_level') %}
          {% if enum_lvl == "VERY_CHEAP" %}
            1 | int(0)
          {% elif enum_lvl == "CHEAP" %}
            2 | int(0)
          {% elif enum_lvl == "NORMAL" %}
            3 | int(0)
          {% elif enum_lvl == "EXPENSIVE" %}
            4 | int(0)
          {% else %} # enum_lvl == "VERY_EXPENSIVE"
            5 | int(0)
          {% endif %}
      - name: Electricity Price - 5 Levels (combined)
        icon: mdi:currency-usd
        state: >-
          {% set level1 = states('sensor.electricity_price_5_levels_24h') %}
          {% set level3 = states('sensor.electricity_price_5_levels_tibber_3_days') %}
          {% if level1 == level3 %}
            {{ level1 }}
          {% elif level1 in ['unknown','undefined','none'] or level3 in ['unknown','undefined','none'] %}
           -1  | int(0)
          {% elif level1 == "1" %}
            {{ level3 }} | int(0)
          {% elif level3 == "1" %}
            {{ level1 }} | int(0)
          {% elif level1 == "2" %}
            {{ level3 }} | int(0)
          {% elif level3 == "2" %}
            {{ level1 }} | int(0)
          {% elif level1 == "3" %}
            {{ level3 }} | int(0)
          {% elif level3 == "3" %}
            {{ level1 }} | int(0)
          {% elif level1 == "4" %}
            {{ level3 }} | int(0)
          {% else %}
            {{ level1 }} | int(0)
          {% endif %}

Note that I’ve converted “unknown” to “-1”, just because I don’t know how the “unknown” will affect your plot.
Try it and let me know. I can play a bit with this later.

Very much - thank you :smiley:

Thanks for your edited code! :sunflower: I tried your new code and now it looks like this:
image

Need to feed the family now. Will tinker more later. :canned_food:

Eureka! :dizzy:
https://www.home-assistant.io/dashboards/history-graph/ says that when a unit_of_measurement is defined it presents a graph.
So now it looks like I want it to:

And the code got reverted and unit_of_measurement added:

template:
  - sensor:
      - name: Electricity Price Level (24h)
        unit_of_measurement: ""
        icon: mdi:currency-usd
        state: >-
          {% set price_cur = states('sensor.electricity_price_majbosv6') | float(0) %}
          {% set price_avg = state_attr('sensor.electricity_price_majbosv6', 'avg_price') | float(0) %}
          {% if price_cur == 0 or price_avg == 0 %}
            -1
          {% else %}
            {% set price_ratio = (price_cur / price_avg) %}
            {% if price_ratio >= 1.4 %}
              5
            {% elif price_ratio >= 1.15 %}
              4
            {% elif price_ratio <= 0.6 %}
              1
            {% elif price_ratio <= 0.9 %}
              2
            {% else %}
              3
            {% endif %}
          {% endif %}
      - name: Electricity Price Level (Tibber 3-days)
        unit_of_measurement: ""
        icon: mdi:currency-usd
        state: >-
          {% set enum_lvl = state_attr('sensor.electricity_price_majbosv6', 'price_level') %}
          {% if enum_lvl == "VERY_CHEAP" %}
            1
          {% elif enum_lvl == "CHEAP" %}
            2
          {% elif enum_lvl == "NORMAL" %}
            3
          {% elif enum_lvl == "EXPENSIVE" %}
            4
          {% else %} # enum_lvl == "VERY_EXPENSIVE"
            5
          {% endif %}
      - name: Electricity Price Level (combined)
        unit_of_measurement: ""
        icon: mdi:currency-usd
        state: >-
          {% set level1 = states('sensor.electricity_price_level_24h') %}
          {% set level3 = states('sensor.electricity_price_level_tibber_3_days') %}
          {% if level1 == level3 %}
            {{ level1 }}
          {% elif level1 in ['unknown','undefined','none'] or level3 in ['unknown','undefined','none'] %}
            -1
          {% elif level1 == "1" %}
            {{ level3 }}
          {% elif level3 == "1" %}
            {{ level1 }}
          {% elif level1 == "2" %}
            {{ level3 }}
          {% elif level3 == "2" %}
            {{ level1 }}
          {% elif level1 == "3" %}
            {{ level3 }}
          {% elif level3 == "3" %}
            {{ level1 }}
          {% elif level1 == "4" %}
            {{ level3 }}
          {% else %}
            {{ level1 }}
          {% endif %}
1 Like

My next steps will be to adapt the code to my CTC heater, which only takes 4 levels as input. Also, I want to add variables so it’s easier to adjust each levels threshold. Something like this in dashboard:
image
“Entities Card Configuration”

1 Like

Cant get this to work… How do you add this to HA ?

Create a new automation in the UI. In the top right corner, click the overflow menu and select “Edit in YAML”. Now paste the example code I provided, replacing existing trigger: and action:. You should then be able to switch back to UI mode.

1 Like

´You the best ! I will try that ! Can i add more “entity_id”: under target ?´

As you are using numbers, you can simplify the combined sensor by using the max function.

Something like this:

      - name: Electricity Price Level (combined)
        unit_of_measurement: ""
        icon: mdi:currency-usd
        state: >-
          {% set level1 = states('sensor.electricity_price_level_24h') | float(-1) %}
          {% set level3 = states('sensor.electricity_price_level_tibber_3_days') | float(-1) %}
          {{ max(level1,level3) }}

I’m not sure if I understood what you are trying to achieve. What those thresholds are doing?

You should be able to do it like this:

    target:
      entity_id:
        - switch.your_switch
        - switch.other_switch
1 Like

Thanks ! :heart_eyes:

Thanks for the reply! You reminded me to update here at the community as well :blush:
I optimized my code a bit and will not be using “Tibber level 3 days”.
Please have a look here:

1 Like

The thresholds are similar to what Tibber are using, here from Tibber Developer Reference docs.
I am not sure how these thresholds will adapt to my 4 level needs… :thinking:
So instead, I made my thresholds variables that I can change when/if I need to tune the settings of the thresholds. I hope it made it a bit more clear to you :smiley:
Please also have a look at my updated and commented code here:
CTC i350 (heat pump) smartcontrol using Tibber electricity prices by the hour

This is a very interesting thread!

I am using the cheap / very cheap attribute from tibber to charge my home battery when there will not be enough solar power this day.

But since I run out of battery almost every day now in the winter, I am thinking about changing the automation:

I want to charge the battery when the electric price is at the daily lowest. I think that should be the attribute “min price”.

Does anyone has an idea how to make a binary switch with “min price”?

1 Like

Hi,
I just started completely new into HA and have no clue so far. I’m trying to get an overview on how this whole thing works in general ^^
I am thinking about the same idea - charging my battery with grid power when the price is low and tanking it out again when it’s bigherly priced.
If you have a solution I’d be very glad to participate.
Sadly at my current knowledge level, I won’t have a chance to help here ^^

In his apexchart this guy is also implementing a minimum price, should be easy enough to create a sensor that compares that value (on state change (see above) of the current price) and sets it to 1 if equal or 0 if not.

Actually, the same guy :smiley: Edward is everywhere :smiley:

Also I just found out that tibber is indeed giving min/max/avg price - I just looked at two different sensors and could not find the attribute -.-

1 Like

Hello Edward,
I am new to HA. Where do I put the code exactly?
I am also strugfgling to automate due to the “3-day” price level. I need an entity with a “todays price level”.
Thank you in advance

You should take a look at the HA documentation (or a video) about HA sensors.
This

Is showing a price level sensor for 24hrs…

The code goes into your configuration.yaml or into its own yaml if you include it (See Splitting up the configuration - Home Assistant)