Nordpool integration - setup of sensor for Denmark with 3 tarif rates per day changing month by month

The added ,0 should set the float to zero if it is not defined, but I am not completely sure of the syntax format.

I am using the Developer Tools to verify the template. Does it make a difference as compared to actually trying this I “real life” in HA?

No idea actually.

The syntax to get 0 as default if the last value may not always be defined should be

- float(this.attributes.last, 0)

Perfect.
As I said I could not remember how the syntax is, but it should then solve the cyclic issue.

I was doing some experiments and found that my triggered template sensor will not work unless it has been defined before and have a historic value

So here is a triggered sensor that calculates the money spent for each interval.

Hope is can inspire someone

- trigger:
    - platform: state
      entity_id: sensor.kamstrup_tpi
  sensor:
    - name: "Electricity Spend"
      unique_id: electricity_spend
      #device_class: monetary
      unit_of_measurement: "DKK"
      state: >
        {% set price = float(states('sensor.energi_data_service'),0) %}
        {% set meter = float(states('sensor.kamstrup_tpi'),0) %}
        {% if meter > 0 and this.attributes.last is defined %}
           {% set delta = meter - float(this.attributes.last,0) %}
        {% else %}
           {% set delta = 0 %}
        {% endif %}
        {{ '%0.6f'|format( price * delta  ) }}
      attributes:
        last: >
          {% if this.attributes.last is defined %}
            {% set lastlast = float(this.attributes.last,0) %}
          {% else %}
            {% set lastlast = 0 %}
          {% endif %}
          {% set meter = float(states('sensor.kamstrup_tpi'),0) %}
          {% if meter > 0 %}
            {{ meter }}
          {% else %}
            {{ lastlast }}
          {% endif %}
1 Like

Tried the new template and it works perfectly. And can be a big inspiration.

I am using the data to feed into a number of Utility Meters as delta values.

When the yaml file(s) are reloaded the latest value is added to the number (of course - as I use the delta function). But when the template triggers, the number is added again, so that the total number is increasing every time the yaml is reloaded - on top what is added by the normal trigger function. It this way the number I the Utility Meters get too high.

I can of course use the service “Utility Meter recalibrate” and subtract the number for a single period that was added “by mistake”

I am considering what to do (if it is at all possible) but some input would be valuable.

I might also stop experimenting and therefore reload yaml files often. :slight_smile: but that is a part of playing around with HA, from which I get a lot of fun.

Hi. I wanted to do the same, but I don’t have any experience with it. I changes the sensor names, but I am not sure how to format it in the templates file. I have a file templates.yaml that is linked to the configuration.yaml file. The templates.yaml contains already a lot of additional configuration and works fine. But how do I add this to the file? How to properly format it? Because if I put it like this

- trigger:
    - platform: state
      entity_id: sensor.solax_feedin_energy
  sensor:
    - name: "Cena vyrobene elektriny"
      unique_id: electricity_produced_price
      #device_class: monetary
      unit_of_measurement: "CZK"
      state: >
        {% set price = float(states('sensor.current_spot_electricity_price'),0) %}
        {% set meter = float(states('sensor.solax_feedin_energy'),0) %}
        {% if meter > 0 and this.attributes.last is defined %}
           {% set delta = meter - float(this.attributes.last,0) %}
        {% else %}
           {% set delta = 0 %}
        {% endif %}
        {{ '%0.6f'|format( price * delta  ) }}
      attributes:
        last: >
          {% if this.attributes.last is defined %}
            {% set lastlast = float(this.attributes.last,0) %}
          {% else %}
            {% set lastlast = 0 %}
          {% endif %}
          {% set meter = float(states('sensor.solax_feedin_energy'),0) %}
          {% if meter > 0 %}
            {{ meter }}
          {% else %}
            {{ lastlast }}
          {% endif %}

The configuration check says that the configuration is invalid

Error loading /config/configuration.yaml: while parsing a block mapping
in “/config/templates.yaml”, line 1, column 1
expected , but found ‘-’
in “/config/templates.yaml”, line 273, column 1

And the line 273 is the line with - trigger:

The templates.yaml file contains only sensors so far, so the content of the file looks like this

sensor:
    - name: "Celkova spotreba elektriny"
      unique_id: CelkovaSpotrebaElektriny
      unit_of_measurement: "W"
      device_class: power
      state: >
        {{ [ states('sensor.faze1_hneda_power'),
             states('sensor.faze2_cerna_power'),
             states('sensor.faze3_seda_power') ]
             | map('float') | sum }}
      availability: >
        {{ not 'unavailable' in 
           [ states('sensor.faze1_hneda_power'), 
             states('sensor.faze2_cerna_power'),
             states('sensor.faze3_seda_power') ] }}
    - name: "Celkova denni spotreba energie"
      unique_id: CelkovaDenniSpotrebaEnergie
      unit_of_measurement: "kWh"
      device_class: energy
      state: >
        {{ [ states('sensor.energy_selfiehome_hneda_usage_daily'),
             states('sensor.energy_selfiehome_cerna_usage_daily'),
             states('sensor.energy_selfiehome_seda_usage_daily') ]
             | map('float') | sum }}
      availability: >
        {{ not 'unavailable' in 
           [ states('sensor.energy_selfiehome_hneda_usage_daily'), 
             states('sensor.energy_selfiehome_cerna_usage_daily'),
             states('sensor.energy_selfiehome_seda_usage_daily') ] }}

In configuration.yaml I have this line

template: !include_dir_merge_list template_sensors

Then I have a directory called template_sensors

Inside this directory are 3 files. Names of files can be anything but must end with .yaml

template_sensors.yaml
template_binary_sensors.yaml
electricity_meter_triggered.yaml

syntax in template_sensors starts with

- sensor:
   
  - name: Test Sensor
    unique_id: test_sensor
    unit_of_measurement: "Bananas"
    state: >
      {{ states('input_number.test_number') }}
      
  - name: Kitchen Temperature Corrected
    unique_id: kitchen_temperature_corrected
    unit_of_measurement: "°C"
    state: >
      {{ (states('sensor.kitchen_temperature')|float(0.0) + 3) |round(1) }}
      

template_binary_sensors.yaml starts with

- binary_sensor:
  - name: Amplifier Power State
    unique_id: amplifier_power_state
    state: >
      {{ states('sensor.blitzwolf_power')| int(default=0) > 15 }}

  - name: Night Time
    unique_id: night_time
    state: >
      {{ not '06:30' <= as_timestamp(now()) | timestamp_custom('%H:%M') <= '23:00' }}
   

and finally electricity_meter_triggered.yaml

I have same sensor two ways with and without device_class as I experiment with how it is displayed

- trigger:
    - platform: state
      entity_id: sensor.kamstrup_tpi
  sensor:
    - name: "Electricity Spend"
      unique_id: electricity_spend
      #device_class: monetary
      unit_of_measurement: "DKK"
      state: >
        {% set price = float(states('sensor.energi_data_service'),0) %}
        {% set meter = float(states('sensor.kamstrup_tpi'),0) %}
        {% if meter > 0 and this.attributes.last is defined %}
           {% set delta = meter - float(this.attributes.last,0) %}
        {% else %}
           {% set delta = 0 %}
        {% endif %}
        {{ '%0.6f'|format( price * delta  ) }}
      attributes:
        last: >
          {% if this.attributes.last is defined %}
            {% set lastlast = float(this.attributes.last,0) %}
          {% else %}
            {% set lastlast = 0 %}
          {% endif %}
          {% set meter = float(states('sensor.kamstrup_tpi'),0) %}
          {% if meter > 0 %}
            {{ meter }}
          {% else %}
            {{ lastlast }}
          {% endif %}


- trigger:
    - platform: state
      entity_id: sensor.kamstrup_tpi
  sensor:
    - name: "Electricity Spend 2"
      unique_id: electricity_spend_2
      device_class: monetary
      unit_of_measurement: "DKK"
      state: >
        {% set price = float(states('sensor.energi_data_service'),0) %}
        {% set meter = float(states('sensor.kamstrup_tpi'),0) %}
        {% if meter > 0 and this.attributes.last is defined %}
           {% set delta = meter - float(this.attributes.last,0) %}
        {% else %}
           {% set delta = 0 %}
        {% endif %}
        {{ '%0.6f'|format( price * delta  ) }}
      attributes:
        last: >
          {% if this.attributes.last is defined %}
            {% set lastlast = float(this.attributes.last,0) %}
          {% else %}
            {% set lastlast = 0 %}
          {% endif %}
          {% set meter = float(states('sensor.kamstrup_tpi'),0) %}
          {% if meter > 0 %}
            {{ meter }}
          {% else %}
            {{ lastlast }}
          {% endif %}

There are multiple ways to do this. This is how I chose to do it

1 Like

Looks good, thank you very much! Just out of curiosity - why do you not use the device_class: monetary attribute?

A bit of a special case.

I am Danish but I prefer to have any computer in my life in British English because I work in a company with computers all English UI and my wife is Canadian. British because I want 24 hour clock and a date format I can understand. So all computers I use have British English locate but Danish keyboard so I can type æøåÆØÅ.

With English comes the rules for writting numbers with a currency unit.

You wite GBP 10.00 or USD 10.00.

But in Danish it is 10.00 DKK. Or 10.00 kr. We put the unit after the number. When I remove the monetary class HA does not know it is money and put the unit after the number just like any other more scientific unit.

There was also a bug in HA a month ago where I needed to analyse what caused issues with the rounding of numbers and that is why I ended up with both. But the one I use now is with monetary commented out so I have my DKK after the number.

1 Like

Understood, thanks a lot! We in Czechia use the currency unit after the numer as well (10.00 CZK or 10.00 Kč), so that makes sense to me too. Have a wonderful day!

I restarted Home Assistant and it all got reset / zeroed. Is it expected? How to keep the values permanently?

The key values here are

sensor.kamstrup_tpi Is a sensor from a device that reads the data from an electricity meter and this sensor is the current absolute reading updated every 10 seconds

See GitHub - UtilitechAS/amsreader-firmware: ESP8266 and ESP32 compatible firmware to read, interpret and publish data to MQTT from smart electrical meters, both DLMS and DSMR is supported for info

So when HA restarts this sensor is an absolute value in kWh with two decimals

The price comes from a custom componets that gets prices from an online resource.

So that becomes a very small amount every 10 seconds which I then feed to two utility meters for hourly and daily spend. I my miss a few cents (øre) during a HA restart but I do not need precision for the hourly and daily cost. After max 10 seconds the numbers continue being added to the The long term statistics are handled by feeding the electricity meter and prices into the energy dashboard and it will just jump up to the new absolute value from the meter and not miss anything.

Utility meter helpers do not reset when you reset home assistant. That is the point of then

1 Like

Oh yes one important thing. The utility meter must be created with the incremental source. This way you may add 0 but the utility meter keeps its value and then at the next data point values are added.

1 Like

Thanks for a good dialogue. I am using it to implement charging control for my EV, where I use the Nordpool data to charge in the cheapest hours at night. I implement a control, which calculates the number of hours I need to charge, and turns on and off the charge box in the cheapest hours at night. This is possible because the charge box has an external port to control it, and I can read the amount of power needed directly from the car (Mercedes EQB integration into Home Assistant HACS).

I have just one addition to the dialogue: I do not believe it is entirely correct that you pay only 5,7 øre pr kWh when buying from Andel. I called their customer service, and got the figure 17 øre, which also corresponds to my own measurements. I have changed supplier to Vindstød, who charges less than 1 øre pr kWh, and announce this also on their web site. All ather suppliers (incl Andel) keep this number secret. With a consumption of 500+ kWh per month, it sums up to more than 1000 kr per year.

I think Andel has an older agreement which I an on and a newer. If I ever get pushed to the new I will change too

For the record. Andel did the silly thing and pushed old customers to newer more expensive plan. So I changed to Vindstød. I am not getting screwed by Andel Energi.

1 Like

Hi @KennethLavrsen
Thanks to this share in post 50. I’m searching for long time the delta solution to energy price to backup in LTS utility meter change and old energy price of provider :wink:

Which is the better solution for energy meter as input of your sensor electricity_spend ?

  • energy daily utility meter ?
  • energy sensor (source of the energy daily utility meter) which showing an index of energy devices

I think the result will be the same…
What is your opinion ?

Appreciate all the work. But I end up with prices very different from the Watts app :frowning: