As I am not a programmer this took some testing to put together correctly. I hope it may be useful for others. The variable price of power in Norway (per kWh) is made up of two parts, the energy itself and for use of the grid. The energy price change every hour, the grid use has a low and a high price, depending of time of day and if it is a workday or not. The energy price is simple - i get that from an integration (Tibber or Nordpool). The grid part I have to add myself, and this Template sensor does the job for me, with two helpers “input number” to make it possible to change the price when updated without editing yaml code. Finally I use a helper that sums the two parts, and that is what i use in Dynamic Energy Cost.
This is in my sensor.yaml file (can also be in configuration.yaml):
- platform: template
sensors:
bkk_nettleie:
friendly_name: BKK Nettleie
unit_of_measurement: NOK/kWh
value_template: >
{% set tariff = { "dag": states('input_number.bkk_nettleie_dag'), "natt": states('input_number.bkk_nettleie_natt_helg') } %}
{% if is_state('sensor.calender.norway', 'On') %}
{{ tariff.natt }}
{% elif now().weekday() >= 5 %}
{{ tariff.natt }}
{% elif now().hour >=6 and now().hour < 22 %}
{{ tariff.dag }}
{% else %}
{{ tariff.natt }}
{% endif %}