Back to original problem
We want to calculate and display cost per time unit like hour or day
Again a reminder. My rock solid datainput is the actual meter reading from a Kamstrup meter which is a number in kWh with 2 decimals accuracy. It updates every 10 seconds. Except if the little ESP8266 choke and skips a reading but then next reading is an absolute reading and we catch up again.
I have changed from normal template sensor to a triggered template sensor.
In my configuration yaml I !include template: from a directory and I simply added a new file for triggered template sensor and the only one I have looks like this
- trigger:
- platform: state
entity_id: sensor.kamstrup_tpi
sensor:
- name: "Electricity Spend"
unique_id: electricity_spend
device_class: monetary
unit_of_measurement: "DKK"
state: >
{{ '%0.6f'|format( float(states('sensor.energi_data_service'),0) * ( float(states('sensor.kamstrup_tpi'),0) - float(this.attributes.last) ) ) }}
attributes:
last: >
{{ float(states('sensor.kamstrup_tpi'),0) }}
Let me explain.
So we trigger each time the Kamstrup meter has a new value. Ie every 10 seconds.
The sensor defined is named Electricity Spend and gets the entity ID sensor.electricity_spend
This is an incremental sensor that sends the amount in DKK spent since last reading.
The sensor has one attribute called last. And last is set to the same value that also triggered the sensor. But as it seems to work, the “this” object used in the state contains the object as it was before the trigger. So we can store the last value that the sensor was triggered for as an attribute and then next time we trigger we calculate the, current - last. And multiply with the price.
I have found that the price from the Energy Data Service works without glitches. At least so far. Good job Malene! But remember to add your local electricity “pusher” over-charge excl VAT to get an accurate figure.
Let us see how this works out. Note that I do not use round(). I could not make that work reliable and it also sucks to look at. I changed to the format and used 6 digits because we want to avoid accumulating errors. But without you get like 20 digits and that is hopeless to look at in a dash board.
To get the daily and hourly I create from helpers two utility_meters. One that resets hourly and one that resets daily. And the source is the electricity_spend sensor. And remember to tick off that it is a delta sensor.
Kenneth