looking at the code it could be a cyclic reference issue.
“this” is referring to the current sensor, but on setting up the state the first time, there is no attribute called last, so it fails.
This is just a guess though.
You may be right. How do I fix that. Thank you
Remove the below text from the setup, then save and restart.
Now the value is created, so re-add the text again and save and restart.
Come to think of it maybe the float text just need a default value.
I can’t remember the syntax though, so it will be a guess, but try to add ,0 to the end.
- float(this.attributes.last),0
@WallyR
Worked just fine as suggested in post 43. Thank you very much
The “,0” in the end didn’t make any difference as suggested in post 44
Isn’t there a better way to do it. Such as checking for the attribute being defined or not before creating the sensor. The first run could then be without the - float(this.attributes.last) whereas the following runs could be with everything included. Perhaps a if-else condition.
As you can see I am not very good at templating, so take it for what it is, please
But thank you very much for your solution.
and thank you to @KennethLavrsen for the template in the first place
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 %}
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. 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
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.
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
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
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.