Template for calculating seasonal or time-based electricity tariffs

Hi!

I am sure there are other (better) solutions to this, but I have created a template that works for me.

Note: This template requires the Workday integration to be set up (Workday - Home Assistant).

The use case is that we pay a variable price for electricity and have great data for this price, but the utilities company also adds a fee on every kWh delivered. In the summer, when the price of electricity is low, this on-top tax can make up more than double of the price/kWh.

This template renders the current tariff added by the utilities company, in my case BKK (Noray), based on if it is a weekend day, a holiday, time of day and season. As far as I can tell, it should properly render the price as per this matrix (up to date as per 08/21/23):

øre/kWH ____________ Day (06-22): ________ Night and weekend/holiday:
January - March: ________ 47.38 __________________ 35.58
April - December: ________55.73 __________________ 43.93

The template is compatible with the nordpool integration, so you can copy paste it to the additional costs template field when you set that up in order to have a sensor for the actual total cost of electricity.

If you have an idea on how to simplify (or correct errors) in the template, please do share! :slight_smile:

{% set el_taxes = 0 %}

{# January to March - Weekend and holiday tariff #}
{% if is_state('binary_sensor.workday_sensor', 'off') and (now().month >= 1 and now().month <= 3) %}
  {% set el_taxes = 35.58 %}

{# April to December - Weekend and holiday tariff #}
{% elif is_state('binary_sensor.workday_sensor', 'off') and now().month >= 4 %}
  {% set el_taxes = 43.93 %}

{# January to March - Day tariff #}
{% elif (now().month >= 1 and now().month <= 3) and (now().hour >= 6 and now().hour <= 22) %}
  {% set el_taxes = 47.38 %}

{# January to March - Night tariff #}
{% elif (now().month >= 1 and now().month <= 3) and (now().hour >= 23 or now().hour <= 5) %}
  {% set el_taxes = 35.58 %}

{# April to December - Day tariff #}
{% elif (now().month >= 4) and (now().hour >= 6 and now().hour <= 22) %}
  {% set el_taxes = 55.73 %}

{# April to December - Night tariff #}
{% elif (now().month >= 4) and (now().hour >= 23 or now().hour <= 5) %}
  {% set el_taxes = 43.93 %}

{% endif %}

{{ ((el_taxes | float) / 100) | float }}

Made a more effecient version of the template, where variables are used.
A little better readability also…

{% set el_taxes = 0 %}
{% set workday = states('binary_sensor.workday_sensor') %}
{% set month = now().month %}
{% set hour = now().hour %}

{# January to March - Weekend and holiday tariff #}
{% if workday == 'off' and (month >= 1 and month <= 3) %}
  {% set el_taxes = 35.58 %}

{# April to December - Weekend and holiday tariff #}
{% elif workday == 'off' and month >= 4 %}
  {% set el_taxes = 43.93 %}

{# January to March - Day tariff #}
{% elif (month >= 1 and month <= 3) and (hour >= 6 and hour <= 22) %}
  {% set el_taxes = 47.38 %}

{# January to March - Night tariff #}
{% elif (month >= 1 and month <= 3) and (hour >= 23 or hour <= 5) %}
  {% set el_taxes = 35.58 %}

{# April to December - Day tariff #}
{% elif (month >= 4) and (hour >= 6 and hour <= 22) %}
  {% set el_taxes = 55.73 %}

{# April to December - Night tariff #}
{% elif (month >= 4) and (hour >= 23 or hour <= 5) %}
  {% set el_taxes = 43.93 %}

{% endif %}

{{ ((el_taxes | float) / 100 )}}

Another update…

This version takes aim at creating a complete calculation of the price of electricity, including taxes and subsidies. This is a norwegian specific function of calculating the price of electricity, where the government subsidizes the price above a certain level.

As before the template requires the workday integration and is set up with the prices and timings of BKK as provider of power lines/infrastructure (didn’t know of a good word for “nettleverandør”). :slight_smile:

{% set el_taxes = 0 %}
{% set workday = states('binary_sensor.workday_sensor') %}
{% set month = now().month %}
{% set hour = now().hour %}

{# January to March - Weekend and holiday tariff #}
{% if workday == 'off' and (month >= 1 and month <= 3) %}
  {% set el_taxes = 0.3558 %}

{# April to December - Weekend and holiday tariff #}
{% elif workday == 'off' and month >= 4 %}
  {% set el_taxes = 0.4393 %}

{# January to March - Day tariff #}
{% elif (month >= 1 and month <= 3) and (hour >= 6 and hour <= 22) %}
  {% set el_taxes = 0.4738 %}

{# January to March - Night tariff #}
{% elif (month >= 1 and month <= 3) and (hour >= 23 or hour <= 5) %}
  {% set el_taxes = 0.3558 %}

{# April to December - Day tariff #}
{% elif (month >= 4) and (hour >= 6 and hour <= 22) %}
  {% set el_taxes = 0.5573 %}

{# April to December - Night tariff #}
{% elif (month >= 4) and (hour >= 23 or hour <= 5) %}
  {% set el_taxes = 0.4393 %}
{% endif %}

{% set subs = (current_price - 0.875) * 0.9 %}

{% set additional_cost = el_taxes - subs %}

{{ additional_cost }}

And yet another update.
It was pointed out to me that the way I had implemented the subsidies made it possible to have a negative price of electricity.
This latest version solves that.

    {% set el_taxes = 0 %}
    {% set workday = states('binary_sensor.workday_sensor') %}
    {% set month = now().month %}
    {% set hour = now().hour %}

    {# January to March - Weekend and holiday tariff #}
    {% if workday == 'off' and (month >= 1 and month <= 3) %}
      {% set el_taxes = 0.3558 %}

    {# April to December - Weekend and holiday tariff #}
    {% elif workday == 'off' and month >= 4 %}
      {% set el_taxes = 0.4393 %}

    {# January to March - Day tariff #}
    {% elif (month >= 1 and month <= 3) and (hour >= 6 and hour <= 22) %}
      {% set el_taxes = 0.4738 %}

    {# January to March - Night tariff #}
    {% elif (month >= 1 and month <= 3) and (hour >= 23 or hour <= 5) %}
      {% set el_taxes = 0.3558 %}

    {# April to December - Day tariff #}
    {% elif (month >= 4) and (hour >= 6 and hour <= 22) %}
      {% set el_taxes = 0.5573 %}

    {# April to December - Night tariff #}
    {% elif (month >= 4) and (hour >= 23 or hour <= 5) %}
      {% set el_taxes = 0.4393 %}
    {% endif %}


    {% if current_price > 0.875 %}
        {% set subs = (current_price - 0.875) * 0.9 %}
        {% set additional_cost = el_taxes - subs %}
    {% elif current_price <= 0.875 %}
        {% set additional_cost = el_taxes %}
    {% endif %}


    {{ additional_cost }} 
1 Like

And a final (I hope) update…
The previous version had no way of knowing if tomorrow was a holiday, and so it would not render the correct additional cost for next days prices.

Two dependecy changes from the previous versions:

  • The workday integration is no longer needed.
  • The Holidays Calendar integration (Bruxy70 Holidays) is now a dependency.

This template allows the integration to pass the date for the hour when the price is considered, meaning that if it is calculating the price for an hour-period tomorrow, the tempalte will check if the date for that hour is in the holiday calendar. Depending on this the additional cost calculation is rendered differently.

I hope it works for you too! :slight_smile:

    {# January to March - Weekend and holiday taxes #}
    {% if (as_timestamp(now()) | timestamp_custom("%F")) in (state_attr('calendar.holidays','holidays') | list) and (now().month >= 1 and now().month <= 3) %}
      {% set el_taxes = 0.3558 %}

    {# April to December - Weekend and holiday taxes #}
    {% elif (as_timestamp(now()) | timestamp_custom("%F")) in (state_attr('calendar.holidays','holidays') | list) and (now().month >= 4) %}
      {% set el_taxes = 0.4393 %}

    {# January to March - Dyatime taxes #}
    {% elif (now().month >= 1 and now().month <= 3) and (now().hour >= 6 and now().hour <= 22) %}
      {% set el_taxes = 0.4738 %}

    {# January to March - Night taxes #}
    {% elif (now().month >= 1 and now().month <= 3) and (now().hour >= 23 or now().hour <= 5) %}
      {% set el_taxes = 0.3558 %}

    {# April to December - Day taxes #}
    {% elif (now().month >= 4) and (now().hour >= 6 and now().hour <= 22) %}
      {% set el_taxes = 0.5573 %}

    {# April to December - Night taxes #}
    {% elif (now().month >= 4) and (now().hour >= 23 or now().hour <= 5) %}
      {% set el_taxes = 0.4393 %}
    {% endif %}

    {# Calculate the subsidies and add the taxes #}
    {% if current_price > 0.875 %}
        {% set subs = (current_price - 0.875) * 0.9 %}
        {% set additional_cost = el_taxes - subs %}
    {% elif current_price <= 0.875 %}
        {% set additional_cost = el_taxes %}
    {% endif %}

    {{ additional_cost | round(4) }}
3 Likes

Just to keep this updated, here is the template with updated prices for variable taxes and correct calculation of the subsidies.
I’ve simplified it a bit, hopefully making it easier to adjust in the future when tax and subsidies change.

Enjoy!

{% set taxes = {
    "jan_mar_night": 0.3786,
    "jan_mar_day": 0.5025,
    "apr_dec_night": 0.4652,
    "apr_dec_day": 0.5925,
    "holiday_jan_mar": 0.3786,
    "holiday_apr_dec": 0.4652
} %}

{% set is_holiday = (as_timestamp(now()) | timestamp_custom("%F")) in (state_attr('calendar.holidays','holidays') | list) %}
{% set season = "jan_mar" if now().month <= 3 else "apr_dec" %}
{% set time_of_day = "day" if now().hour >= 6 and now().hour <= 22 else "night" %}
{% set el_taxes = taxes["holiday_" ~ season] if is_holiday else taxes[season ~ "_" ~ time_of_day] %}

{# Calculate the subsidies #}
{% set subsidy_rate = 0.9 if (now().month in [1, 2, 3, 10, 11, 12]) else 0.8 %}
{% if current_price > 0.70 %}
    {% set subs = (current_price - 0.70) * subsidy_rate %}
    {% set additional_cost = el_taxes - subs %}
{% else %}
    {% set additional_cost = el_taxes %}
{% endif %}

{{ additional_cost | round(4) }}

It took me up until now to realise this template did not take weekends into account.
This fixes that.

{% set taxes = {
    "jan_mar_night": 0.3786,
    "jan_mar_day": 0.5025,
    "apr_dec_night": 0.4652,
    "apr_dec_day": 0.5925,
    "holiday_jan_mar": 0.3786,
    "holiday_apr_dec": 0.4652
} %}

{# Determine if today is a holiday #}
{% set today = (as_timestamp(now()) | timestamp_custom("%F")) %}
{% set holidays = state_attr('calendar.holidays', 'holidays') | list %}
{% set is_holiday = today in holidays %}

{# Determine if today is a weekend (Saturday=5, Sunday=6) #}
{% set weekday = now().weekday() %}
{% set is_weekend = weekday >= 5 %}  {# 5=Saturday, 6=Sunday #}

{# Combine holiday and weekend checks #}
{% set is_holiday_or_weekend = is_holiday or is_weekend %}

{# Determine the current season #}
{% set season = "jan_mar" if now().month <= 3 else "apr_dec" %}

{# Determine the time of day #}
{% set time_of_day = "day" if 6 <= now().hour <= 22 else "night" %}

{# Select the appropriate tax rate #}
{% if is_holiday_or_weekend %}
    {% set el_taxes = taxes["holiday_" ~ season] %}
{% else %}
    {% set el_taxes = taxes[season ~ "_" ~ time_of_day] %}
{% endif %}

{# Calculate the subsidies #}
{% set subsidy_rate = 0.9 if now().month in [1, 2, 3, 10, 11, 12] else 0.8 %}
{% if current_price > 0.70 %}
    {% set subs = (current_price - 0.70) * subsidy_rate %}
    {% set additional_cost = el_taxes - subs %}
{% else %}
    {% set additional_cost = el_taxes %}
{% endif %}

{{ additional_cost | round(4) }}