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