NordPool N1 DK additional_costs

Hi All

This is my first attempt at coding in Jinja, so please bare with me :smiley:

Where I live, my net provider is N1 and they are constantly changing their prices depending on the month and time of day. On top of that, the tax to the government is also changing twice next year, and then the following years, it changes once a year.

Is there anyone who is willing to take a look on what I’ve made, to sort out any bugs or you are also more then welcome to use it yourself :smiley:

So. This year, the overall prices changes at 1. October 2022. On top of that, from October to April (including) from 17.00 to 20.00 the prices are even higher.

In 2023, the tax the first 6 month is lower, then the rest of the year, but then again, from October to April, the price from 17.00 to 20.00 is higher.

Then from 2024 an onwards, the tax changes once a year, but again from October to April between 17.00 to 20.00 the price is higher.

I have looked it over again and again and again, but my language of choice is php, so I had to read up on Jinja to be sure I’ve made it the right way.

lavlast_rabat_XXX = low prices discount
spidslast_rabat_XXX = high prices discount
nettarif_lavlast_XX_2022 = low cost kWh for the specific month in 2022
nettarif_spidslast_XX_2022 = high cost kWh for the specific month in 2022
elafgift_2022 = tax 2022
elafgift_H1_2023 = tax first half of 2023
elafgift_H2_2023 = tax second half of 2023
elafgift_202X = tax for the following years
balancering = an additionel tax

additional_costs: '{% set s = {
    "lavlast_rabat_2022": 0.1299,
    "lavlast_rabat_2023": 0,
    "spidslast_rabat_2022": 0.1605,
    "spidslast_rabat_2023": 0,
    "nettarif_lavlast_10_2022": 0.3942,
    "nettarif_lavlast_11_2022": 0.5096,
    "nettarif_lavlast_12_2022": 0.5096,
    "nettarif_lavlast_2023": 0.5403,
    "nettarif_spidslast_10_2022": 1.027,
    "nettarif_spidslast_11_2022": 1.2699,
    "nettarif_spidslast_12_2022": 1.2699,
    "nettarif_spidslast_2023": 1.3202,
    "elafgift_2022": 0.95375,
    "elafgift_H1_2023": 0.01,
    "elafgift_H2_2023": 0.87125,
    "elafgift_2024": 0.8875,
    "elafgift_2025": 0.81,
    "elafgift_2026": 0.81,
    "elafgift_2027": 0.81,
    "elafgift_2028": 0.79,
    "elafgift_2029": 0.77125,
    "elafgift_2030": 0.70125,
    "balancering": 0.008
    }
    %}
        {% if now().year == 2022 %}
          Year 2022 <-- Lines like this was just for "debugging" and should be removed.
          {% if now().month >= 10 or now().month <= 4 %}
            Month 10 to 4
            {% if now().hour <= 20 and now().hour >= 17 %}
              Hour 17 to 20
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ now().month ~ "_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
            {% else %}
              Not hour 17 to 20
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% else %}
            Not month 10 to 4
            {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
          {% endif %}
        {% elif now().year == 2023 %}
          Year 2023
          {% if now().month >= 1 and now().month <= 6 %}
            Month 1 to 6
            {% if now().month >= 10 or now().month <= 4 %}
              Month 10 to 4
                {% if now().hour <= 20 and now().hour >= 17 %}
                  Hour 17 to 20
                    {{(s["elafgift_H1_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ "_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
                {% else %}
                  Not hour 17 to 20
                  {{(s["elafgift_H1_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
                {% endif %}
            {% else %}
              {{(s["elafgift_H1_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% else %}
            {% if now().month >= 10 or now().month <= 4 %}
              Month 10 to 4
                {% if now().hour <= 20 and now().hour >= 17 %}
                  Hour 17 to 20
                    {{(s["elafgift_H2_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ "_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
                {% else %}
                  Not hour 17 to 20
                  {{(s["elafgift_H2_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
                {% endif %}
            {% else %}
              {{(s["elafgift_H2_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% endif %}
        {% else %}
          Not year 2022 or 2023
          {% if now().month >= 10 or now().month <= 4 %}
            Month 10 to 4
            {% if now().hour <= 20 and now().hour >= 17 %}
              Hour 17 to 20
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ now().month ~ "_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
            {% else %}
              Not hour 17 to 20
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% else %}
            {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
          {% endif %}
        {% endif %}'
1 Like

Hi @smulle48 Did you ever get this to work? I tried adding your additional_cost code but got all these errors. I should mention I run HA in docker on raspi.

2022-11-13 09:43:34.654 ERROR (MainThread) [homeassistant.util.logging] Exception in check_stuff when dispatching ‘nordpool_update’: ()
Traceback (most recent call last):
File “/config/custom_components/nordpool/sensor.py”, line 483, in check_stuff
self.async_write_ha_state()
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 533, in async_write_ha_state
self._async_write_ha_state()
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 571, in _async_write_ha_state
state = self._stringify_state(available)
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 539, in _stringify_state
if (state := self.state) is None:
File “/config/custom_components/nordpool/sensor.py”, line 240, in state
return self.current_price
File “/config/custom_components/nordpool/sensor.py”, line 321, in current_price
res = self._calc_price()
File “/config/custom_components/nordpool/sensor.py”, line 294, in _calc_price
price += template_value
TypeError: unsupported operand type(s) for +=: ‘float’ and ‘str’

2022-11-13 09:43:34.666 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform nordpool
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 428, in async_add_entities
await asyncio.gather(*tasks)
File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 691, in _async_add_entity
await entity.add_to_platform_finish()
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 776, in add_to_platform_finish
await self.async_added_to_hass()
File “/config/custom_components/nordpool/sensor.py”, line 490, in async_added_to_hass
await self.check_stuff()
File “/config/custom_components/nordpool/sensor.py”, line 449, in check_stuff
self._update(today)
File “/config/custom_components/nordpool/sensor.py”, line 312, in _update
self._average = self._calc_price(data.get(“Average”))
File “/config/custom_components/nordpool/sensor.py”, line 294, in _calc_price
price += template_value
TypeError: unsupported operand type(s) for +=: ‘float’ and ‘str’
2022-11-13 09:43:34.671 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up nordpool platform for sensor
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 289, in _async_setup_platform
await asyncio.gather(*pending)
File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 428, in async_add_entities
await asyncio.gather(*tasks)
File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 691, in _async_add_entity
await entity.add_to_platform_finish()
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 776, in add_to_platform_finish
await self.async_added_to_hass()
File “/config/custom_components/nordpool/sensor.py”, line 490, in async_added_to_hass
await self.check_stuff()
File “/config/custom_components/nordpool/sensor.py”, line 449, in check_stuff
self._update(today)
File “/config/custom_components/nordpool/sensor.py”, line 312, in _update
self._average = self._calc_price(data.get(“Average”))
File “/config/custom_components/nordpool/sensor.py”, line 294, in _calc_price
price += template_value
TypeError: unsupported operand type(s) for +=: ‘float’ and ‘str’
2022-11-13 09:43:36.017 WARNING (MainThread) [homeassistant.components.sensor.helpers] sensor.cheapest_hours_energy_tomorrow rendered invalid timestamp:

Works fine here. From configuration.yaml. Did remove the debug lines from original post.

sensor:
  - platform: nordpool
    currency: "DKK"
    price_in_cents: false
    region: "DK1"
    precision: 2
    price_type: kWh 
    additional_costs: '{% set s = {
    "lavlast_rabat_2022": 0.1299,
    "lavlast_rabat_2023": 0,
    "spidslast_rabat_2022": 0.1605,
    "spidslast_rabat_2023": 0,
    "nettarif_lavlast_10_2022": 0.3942,
    "nettarif_lavlast_11_2022": 0.5096,
    "nettarif_lavlast_12_2022": 0.5096,
    "nettarif_lavlast_2023": 0.5403,
    "nettarif_spidslast_10_2022": 1.027,
    "nettarif_spidslast_11_2022": 1.2699,
    "nettarif_spidslast_12_2022": 1.2699,
    "nettarif_spidslast_2023": 1.3202,
    "elafgift_2022": 0.95375,
    "elafgift_H1_2023": 0.01,
    "elafgift_H2_2023": 0.87125,
    "elafgift_2024": 0.8875,
    "elafgift_2025": 0.81,
    "elafgift_2026": 0.81,
    "elafgift_2027": 0.81,
    "elafgift_2028": 0.79,
    "elafgift_2029": 0.77125,
    "elafgift_2030": 0.70125,
    "balancering": 0.008
    }
    %}
        {% if now().year == 2022 %}
          {% if now().month >= 10 or now().month <= 4 %}
            {% if now().hour <= 20 and now().hour >= 17 %}
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ now().month ~ "_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
            {% else %}
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% else %}
            {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
          {% endif %}
        {% elif now().year == 2023 %}
          {% if now().month >= 1 and now().month <= 6 %}
            {% if now().month >= 10 or now().month <= 4 %}
                {% if now().hour <= 20 and now().hour >= 17 %}
                    {{(s["elafgift_H1_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ "_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
                {% else %}
                  {{(s["elafgift_H1_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
                {% endif %}
            {% else %}
              {{(s["elafgift_H1_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% else %}
            {% if now().month >= 10 or now().month <= 4 %}
                {% if now().hour <= 20 and now().hour >= 17 %}
                    {{(s["elafgift_H2_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ "_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
                {% else %}
                  {{(s["elafgift_H2_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
                {% endif %}
            {% else %}
              {{(s["elafgift_H2_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% endif %}
        {% else %}
          {% if now().month >= 10 or now().month <= 4 %}
            {% if now().hour <= 20 and now().hour >= 17 %}
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ now().month ~ "_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
            {% else %}
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% else %}
            {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
          {% endif %}
        {% endif %}'


Please note, the configuration does not work for 2022 due to additional underscores. This works for me:

sensor:
  - platform: nordpool
    currency: "DKK"
    price_in_cents: false
    region: "DK1"
    precision: 2
    price_type: kWh 
    additional_costs: '{% set s = {
    "lavlast_rabat_2022": 0.1299,
    "lavlast_rabat_2023": 0,
    "spidslast_rabat_2022": 0.1605,
    "spidslast_rabat_2023": 0,
    "nettarif_lavlast_10_2022": 0.3942,
    "nettarif_lavlast_11_2022": 0.5096,
    "nettarif_lavlast_12_2022": 0.5096,
    "nettarif_lavlast_2023": 0.5403,
    "nettarif_spidslast_10_2022": 1.027,
    "nettarif_spidslast_11_2022": 1.2699,
    "nettarif_spidslast_12_2022": 1.2699,
    "nettarif_spidslast_2023": 1.3202,
    "elafgift_2022": 0.95375,
    "elafgift_H1_2023": 0.01,
    "elafgift_H2_2023": 0.87125,
    "elafgift_2024": 0.8875,
    "elafgift_2025": 0.81,
    "elafgift_2026": 0.81,
    "elafgift_2027": 0.81,
    "elafgift_2028": 0.79,
    "elafgift_2029": 0.77125,
    "elafgift_2030": 0.70125,
    "balancering": 0.008
    }
    %}
        {% if now().year == 2022 %}
          {% if now().month >= 10 or now().month <= 4 %}
            {% if now().hour <= 20 and now().hour >= 17 %}
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ now().month ~ "_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
            {% else %}
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% else %}
            {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
          {% endif %}
        {% elif now().year == 2023 %}
          {% if now().month >= 1 and now().month <= 6 %}
            {% if now().month >= 10 or now().month <= 4 %}
                {% if now().hour <= 20 and now().hour >= 17 %}
                    {{(s["elafgift_H1_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
                {% else %}
                  {{(s["elafgift_H1_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
                {% endif %}
            {% else %}
              {{(s["elafgift_H1_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% else %}
            {% if now().month >= 10 or now().month <= 4 %}
                {% if now().hour <= 20 and now().hour >= 17 %}
                    {{(s["elafgift_H2_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
                {% else %}
                  {{(s["elafgift_H2_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
                {% endif %}
            {% else %}
              {{(s["elafgift_H2_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~  now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% endif %}
        {% else %}
          {% if now().month >= 10 or now().month <= 4 %}
            {% if now().hour <= 20 and now().hour >= 17 %}
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_spidslast_" ~ now().month ~ "_" ~ now().year])-s["spidslast_rabat_" ~ now().year]|float}}
            {% else %}
              {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
            {% endif %}
          {% else %}
            {{(s["elafgift_" ~ now().year]+s.balancering+s["nettarif_lavlast_" ~ now().month ~ "_" ~ now().year])-s["lavlast_rabat_" ~ now().year]|float}}
          {% endif %}
        {% endif %}'
1 Like