Tibber - Schedul prices upcoming 24 hours prices!

unfortunately without success

Everything works here, as described above. Maybe reinstall the integration?

Reloading did seem to help with me. But after some the time price again is unavailable.

There seems to be a problem:
Github for Tibber

problems upon problems… :weary:

Ok, rebooting 3 or 4 times seems to fix the problem…

And i got values now :slight_smile:

…but here still no values :frowning:

Has anyone ever made this sensor work?

What I don’t understand is:

action:
- service: tibber.get_prices < What is behind it? YAML?

How did Ingo create the two?
input_number.tibber_cheap_energy_prices_hours
input_number.tibber_cheap_energy_min_price_difference

binary_sensor:
- unique_id: tibber_next_good_grid_charging_time < How is the YAML for this?
name: tibber_next_good_grid_charging_time

4.) a trigger-binary_sensor that catches the prices for a specific amount of hours and decides if and when there is a good time to charge from grid. The decission is based on:

  • the price-difference between the cheapest and the most expensive price in the time-span
  • if the price-difference is above a specific value (input_number → for me 0.1 = 10 Cents)
template:
  - trigger:
      - platform: time_pattern
        hours: "/1"
      - platform: homeassistant
        event: start
    action:
      - service: tibber.get_prices
        data:
          start: "{{ (now()).strftime('%Y-%m-%d %H:') + '00:00' }}"
          end: "{{ (now() + timedelta(hours=states('input_number.tibber_cheap_energy_prices_hours') | int(default=24))).strftime('%Y-%m-%d %H:%M:%S') }}"
        response_variable: action_response
    binary_sensor:
      - unique_id: tibber_next_good_grid_charging_time
        name: tibber_next_good_grid_charging_time
# replace "Zuhause" with the name of your "home" in the tibber-integration
        state: >
          {% set min_price = action_response["prices"]["Zuhause"] |map(attribute='price') | list | min | float(default=0) %}
          {% set max_price = action_response["prices"]["Zuhause"] |map(attribute='price') | list | max | float(default=0) %}
          {% set lowest_price_entry = action_response["prices"]["Zuhause"] | min(attribute='price') %}
          {% set start_time_lowest_price = lowest_price_entry["start_time"] %}
          {% set end_time_lowest_price = as_datetime(start_time_lowest_price) + timedelta(hours=1) %}
          {{as_datetime(now()) > as_datetime(start_time_lowest_price) and as_datetime(now()) < as_datetime(end_time_lowest_price) and (max_price - min_price) > states('input_number.tibber_cheap_energy_min_price_difference') | float(default=0) }}
        attributes:
          min_price: >
            {% set min_price = action_response["prices"]["Zuhause"] |map(attribute='price') | list | min | float(default=0) %}
            {{min_price}}
          max_price: >
            {% set max_price = action_response["prices"]["Zuhause"] |map(attribute='price') | list | max | float(default=0) %}
            {{max_price}}
          price_range: >
            {% set min_price = action_response["prices"]["Zuhause"] |map(attribute='price') | list | min | float(default=0) %}
            {% set max_price = action_response["prices"]["Zuhause"] |map(attribute='price') | list | max | float(default=0) %}
            {{max_price - min_price}}
          start_time: >
            {% set min_price = action_response["prices"]["Zuhause"] |map(attribute='price') | list | min | float(default=0) %}
            {% set max_price = action_response["prices"]["Zuhause"] |map(attribute='price') | list | max | float(default=0) %}
            {% set lowest_price_entry = action_response["prices"]["Zuhause"] | min(attribute='price') %}
            {% set start_time_lowest_price = lowest_price_entry["start_time"] %}
            {% set end_time_lowest_price = as_datetime(start_time_lowest_price) + timedelta(hours=1) %}
            {{start_time_lowest_price}}
          end_time: >
            {% set min_price = action_response["prices"]["Zuhause"] |map(attribute='price') | list | min | float(default=0) %}
            {% set max_price = action_response["prices"]["Zuhause"] |map(attribute='price') | list | max | float(default=0) %}
            {% set lowest_price_entry = action_response["prices"]["Zuhause"] | min(attribute='price') %}
            {% set start_time_lowest_price = lowest_price_entry["start_time"] %}
            {% set end_time_lowest_price = as_datetime(start_time_lowest_price) + timedelta(hours=1) %}
            {{end_time_lowest_price}}            
          prices: >
            {{action_response["prices"]["Zuhause"] | list}}


That binary_sensor has a few attributes that i need for my dashboard:

* min_price => the minimum price in the fetched time-span (in €, so 0,32 means 32 Cents)
* max_price => the maximum price in the fetched time-span (in €, so 0,40 means 40 Cents)
* price_range => the difference between the min_price and the max_price (in €, so 0,08 means 8 Cents)
* start_time => the start-time of the cheapest energy-hour
* end_time => the end-time of the cheapest energy-hour
* prices => just for information: the start_times, prices and price_levels in the time-span Blockquote

Install this: https://github.com/TheFes/cheapest-energy-hours

Add Helpers named like this

put this code in configuration.yaml

@ingo.niehues
I think there is still a problem with the logic here,
because the min price should always be before the max price, right? :wink:

1 Like

Hi, I get the same error message.
Also with your code.
How did you solve it?

Hi everyone! I’m following this topic now for a few weeks. First of all, I like to give a shout-out @ingo.niehues and everyone else who worked and is still working for this! Really appreciate your effort!
I used Ingo’s code from post nr. 69 and also included most of the followed improvements. When my tibber integration suddenly stopped working a few days ago, I came back to this topic. Luckily I saw the new solution by Ingo (post nr. 250) and I wanted to get this working for me as well.
Unfortunately, I am stuck (like so others) with this binary sensor that decides if and when to charge from the grid. Here’s what I did so far:

  • Created template binary-sensor sensor.tibber_kein_uberschussstrom_vorhanden according Ingo’s code
  • Created template binary-sensor sensor.tibber_speicher_ist_nicht_voll according Ingo’s code
  • Created template binary-sensor sensor.tibber_heute_ist_mit_wenig_solarertrag_zu_rechnen according Ingo’s code
  • Created input-number input_number.tibber_laden_bei_restproduktion_unter
  • Created input-number input_number.tibber_cheap_energy_prices_hours
  • Created input-number input_number.tibber_cheap_energy_min_price_difference

With this being done, I used Ingo’s code for this binary-sensor tibber_next_good_grid_charging_time and included it in my configuration.yaml. The entity appears after a reboot, but with the value unknown. If I use the developer tools and paste the code, it gives me an error message that ‘action’ key is not defined: Keine Aktion definiert. Bitte definiere einen ‘action:’ Schlüssel.

If I try only the ‘action’ part (tibber.get_prices) manually in the developer tools, it works for me and I get a list of prices depending on the value of input_number.tibber_cheap_energy_prices_hours.

For me it looks like there is a problem with “action_response” but I’m not sure why it works for some people but not for everyone.

So, this is where my knowledge ends and I really hope someone can help me and the other guys who share this problem.

I agree too. As a quick fix, I have added following code in the trigger-binary sensor:

state: >
          {% set min_price = action_response["prices"]["xxx"] |map(attribute='price') | list | min | float(default=0) %}
          {% set max_price = action_response["prices"]["xxx"] |map(attribute='price') | list | max | float(default=0) %}
          {% set lowest_price_entry = action_response["prices"]["xxx"] | min(attribute='price') %} 
          {% set highest_price_entry = action_response["prices"]["xxx"] | max(attribute='price') %}
          {% set start_time_lowest_price = lowest_price_entry["start_time"] %}
          {% set start_time_highest_price = highest_price_entry["start_time"] %}
          {% set end_time_lowest_price = as_datetime(start_time_lowest_price) + timedelta(hours=1) %}       
          {{as_datetime(now()) > as_datetime(start_time_lowest_price) and as_datetime(now()) < as_datetime(end_time_lowest_price) and as_datetime(start_time_highest_price) > as_datetime(start_time_lowest_price) and (max_price - min_price) > states('input_number.tibber_cheap_energy_min_price_difference') | float(default=0) }}

I don’t know if it works, I couldn’t test it now :slightly_smiling_face:

Thanks for fast response, let us try and see what happened with this new code :wink:

Does anyone have an idea how to calculate the current price of the energy stored in the battery? So zero or any other value for solar power and the respective Tibber price for charged grid power?

Screenshot 2024-11-14 111956

And here the code:
Note: Replace ‘REPLACE’ with your tibber electricity_price sensor!

type: custom:config-template-card
variables:
  - >-
    ((((states['sensor.electricity_price_REPLACE'].attributes.max_price -
    states['sensor.electricity_price_REPLACE'].attributes.min_price) / 4) *
    0) + states['sensor.electricity_price_REPLACE'].attributes.min_price) *
    100
  - >-
    ((((states['sensor.electricity_price_REPLACE'].attributes.max_price -
    states['sensor.electricity_price_REPLACE'].attributes.min_price) / 4) *
    1) + states['sensor.electricity_price_REPLACE'].attributes.min_price) *
    100
  - >-
    ((((states['sensor.electricity_price_REPLACE'].attributes.max_price -
    states['sensor.electricity_price_REPLACE'].attributes.min_price) / 4) *
    2) + states['sensor.electricity_price_REPLACE'].attributes.min_price) *
    100
  - >-
    ((((states['sensor.electricity_price_REPLACE'].attributes.max_price -
    states['sensor.electricity_price_REPLACE'].attributes.min_price) / 4) *
    3) + states['sensor.electricity_price_REPLACE'].attributes.min_price) *
    100
entities: ${vars[0]}
card:
  type: custom:apexcharts-card
  experimental:
    color_threshold: true
  graph_span: 48h
  yaxis:
    - id: costsKWH
      opposite: false
      align_to: 1
      apex_config:
        tickAmount: 10
        decimalsInFloat: 0
  all_series_config:
    unit: Cent
  apex_config:
    grid:
      show: true
      borderColor: "#E0E0E0"
    tooltip:
      enabled: true
      followCursor: false
      x:
        show: true
      fixed:
        enabled: false
  header:
    show: true
    show_states: true
    colorize_states: true
    standard_format: false
  now:
    show: true
    color: 9E9E9E
    label: now
  span:
    start: day
  series:
    - entity: sensor.tibber_prices
      show:
        in_header: before_now
        name_in_header: true
        header_color_threshold: true
        extremas: true
      name: cost
      color_threshold:
        - value: ${vars[0]}
          color: green
        - value: ${vars[1]}
          color: yellow
        - value: ${vars[2]}
          color: orange
        - value: ${vars[3]}
          color: red
      type: column
      float_precision: 0
      data_generator: >
        const noon = new Date();
        noon.setHours(0, 0, 0, 0);
        const prices =
        entity.attributes.today.concat(entity.attributes.tomorrow);
        const data = [];
        for(let i = 0; i < prices.length; i++) {
          data.push([noon.getTime() + i * 1000 * 3600, prices[i].total * 100])
        }
        return data;
2 Likes

I’m afraid that doesn’t work like that. Shouldn’t it calculate the start time accordingly?

How do I create this sensor?

Why is the sensor called sensor.tibber_prices?

is that not the unique id from above:

    unique_id: tibber_prices_level

homeassistant tells me:
grafik

Althoug I completely restarted the docker container.

Hi, here ist the code ( from ingo.niehues) for the sensor…

configuration.yaml

  # Tibber Vorschaupreise
  - platform: rest
    unique_id: tibber_prices
    name: Tibber Prices
    resource: https://api.tibber.com/v1-beta/gql
    method: POST
    payload: '{ "query": "{ viewer { homes { currentSubscription { status priceInfo { current { total level startsAt } today { total level startsAt } tomorrow { total level startsAt } } } } } }" }'
    json_attributes_path: "$.data.viewer.homes[0].currentSubscription.priceInfo" 
    json_attributes:
      - today
      - tomorrow
    value_template: "{{ value_json.data.viewer.homes[0].currentSubscription.priceInfo.current.total | float }}"
    scan_interval: 300
    headers:
      Authorization: !secret tibber_token
      Content-Type: application/json
      User-Agent: REST
    unit_of_measurement: EUR/kWh
    state_class: total

I am a little lost in the thread :cry:

Added an automation with:

alias: Update Tibber Prices
description: ""
triggers:
  - hours: /1
    trigger: time_pattern
actions:
  - data:
      start: "{{ (now()).strftime('%Y-%m-%d %H:00:00') }}"
      end: >-
        {{ (now() +
        timedelta(hours=states('input_number.tibber_cheap_energy_prices_hours')
        | int(default=24))).strftime('%Y-%m-%d %H:%M:%S') }}
    target:
      entity_id: binary_sensor.tibber_next_good_grid_charging_time
    action: sensor.tibber_prices

Fetching the prices via REST API


  - platform: rest
    name: Tibber prices
    unique_id: tibber_prices
    resource: https://api.tibber.com/v1-beta/gql
    method: POST
    scan_interval: 60
    payload: '{ "query": "{ viewer { homes { currentSubscription { priceInfo { today { total startsAt } tomorrow { total startsAt }}}}}}" }'
    json_attributes_path: "$.data.viewer.homes[0].currentSubscription.priceInfo"
    json_attributes:
      - today
      - tomorrow
    value_template: Ok
    headers:
      Authorization: "XXXXXXXXXXXXXXXXX"
      Content-Type: application/json
      User-Agent: REST

but my binary sensor thorws an error

    binary_sensor:
      - unique_id: tibber_next_good_grid_charging_time
        name: Tibber Next Good Grid Charging Time
        state: >
          {% set min_price = action_response["prices"]["Zuhause"] | map(attribute='price') | list | min | float(default=0) %}
          {% set max_price = action_response["prices"]["Zuhause"] | map(attribute='price') | list | max | float(default=0) %}
          {% set lowest_price_entry = action_response["prices"]["Zuhause"] | min(attribute='price') %}
          {% set start_time_lowest_price = lowest_price_entry["start_time"] %}
          {% set end_time_lowest_price = as_datetime(start_time_lowest_price) + timedelta(hours=1) %}
          {{ as_datetime(now()) > as_datetime(start_time_lowest_price) and as_datetime(now()) < as_datetime(end_time_lowest_price) and (max_price - min_price) > states('input_number.tibber_cheap_energy_min_price_difference') | float(default=0) }}
        attributes:
          min_price: >
            {{ min_price }}
          max_price: >
            {{ max_price }}
          price_range: >
            {{ max_price - min_price }}
          start_time: >
            {{ start_time_lowest_price }}
          end_time: >
            {{ end_time_lowest_price }}
          prices: >
            {{ action_response["prices"]["Zuhause"] | list }}