Skip to content

Instantly share code, notes, and snippets.

@bjornbak
Created October 31, 2022 04:47
Show Gist options
  • Save bjornbak/6454cfa2285bc07ed6ec49e7b680bcfe to your computer and use it in GitHub Desktop.
Save bjornbak/6454cfa2285bc07ed6ec49e7b680bcfe to your computer and use it in GitHub Desktop.
template:
sensor:
- unique_id: energy_buy_prices_dkk_kwh
name: "Energy Buy Prices DKK/kWh"
device_class: energy
unit_of_measurement: DKK/kWh
state: OK
attributes:
prices: >
{% set raw_prices=( state_attr('sensor.energi_data_service_raw', 'raw_today' ) + (state_attr('sensor.energi_data_service_raw', 'raw_tomorrow' ) if state_attr('sensor.energi_data_service_raw', 'tomorrow_valid' ) else []) ) %}
{% set prices_with_fees = namespace(prices=[]) %}
{% for raw_price in raw_prices %}
{% set year = raw_price.hour.year %}
{% set month = raw_price.hour.month %}
{% set hour = raw_price.hour.hour %}
{% set trans_nettarif = 0.06125 if year == 2022 else 0.095 %}
{% set systemtarif = 0.07625 if year == 2022 else 0.08375 %}
{% set balancetarif_forbrug = 0.002875 if year == 2022 else 0.0 %}
{% set elafgift = 0.95375 %} {# 2022 #}
{% if year == 2023 %}
{% set elafgift = 0.01 if month < 7 else 0.87125 %} {# Midlertidig nedsættelse af elafgift #}
{% endif %}
{% if year == 2022 %}
{% if hour >= 17 and hour <= 20 %} {# Spidslast #}
{% set nettarif = 0.956375 %}
{% else %} {# Lavlast #}
{% set nettarif = 0.375375 %}
{% endif %}
{% else %} {# 2023+ #}
{% if month == 1 %} {# Vintertarif 2023-01 #}
{% if hour < 6 %} {# Lavlast #}
{% set nettarif = 0.265875 %}
{% elif hour >= 17 and hour < 21 %} {# Spidslast #}
{% set nettarif = 2.391875 %}
{% else %} {# Højlast (6-17 + 21-24) #}
{% set nettarif = 0.797375 %}
{% endif %}
{% elif month < 4 or month > 9 %} {# Vintertarif 2023-02+ #}
{% if hour < 6 %} {# Lavlast #}
{% set nettarif = 0.286875 %}
{% elif hour >= 17 and hour < 21 %} {# Spidslast #}
{% set nettarif = 2.58325 %}
{% else %} {# Højlast (6-17 + 21-24) #}
{% set nettarif = 0.861125 %}
{% endif %}
{% else %} {# Sommertarif 2023 #}
{% if hour < 6 %} {# Lavlast #}
{% set nettarif = 0.286875 %}
{% elif hour >= 17 and hour < 21 %} {# Spidslast #}
{% set nettarif = 1.119375 %}
{% else %} {# Højlast (6-17 + 21-24) #}
{% set nettarif = 0.4305 %}
{% endif %}
{% endif %}
{% endif %}
{% set prices_with_fees.prices = prices_with_fees.prices + [{'hour': raw_price.hour.isoformat(), 'price': (raw_price.price + nettarif + trans_nettarif + systemtarif + balancetarif_forbrug + elafgift) | round(3) }] %}
{% endfor %}
{{ prices_with_fees.prices }}
- unique_id: energy_current_buy_price_dkk_kwh
name: "Energy Current Buy Price DKK/kWh"
device_class: energy
unit_of_measurement: DKK/kWh
state: >
{{ (state_attr('sensor.energy_buy_prices_dkk_kwh', 'prices')[now().hour+1].price | float(0)) }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment