I have an existing sensor (from an integration) that provides an (always increasing) energy value (kWh). I’d like to create a new virtual sensor (maybe a template sensor?) that shows how much has been added to this sensor since the start of ‘today’. Logic something like:
At 00:00:00 save the value of the lifetime sensor.
Return the current value of the lifetime sensor minus the current saved value.
Is this possible? Any guidance on how to implement this (specifically the saving the value at 00:00:00 so it can be used later)? Or would a different technique be better?
Any guidance gratefully received as I’m still on a steep learning curve regarding HA.
@vingerha Sadly that doesn’t provide what I need (though it has some of the necessary characteristics). The type for this is limited to time, ratio and count none of which are what I need (I need a floating point accumulator). Also, it tracks a ‘state’ not a numeric value.
@Troon Thanks for that. it looks like it should do what I want but I added the following to my configuration.yaml (I did not already have any ‘template:’ sections):
template:
- trigger:
- platform: time
at: "00:00"
sensor:
- name: "Solar export at start of day"
unique_id: solar_export_at_start_of_day
value_template: "{{ states('sensor.jenkins_family_solar_export') }}"
unit_of_measurement: 'kWh'
device_class: energy
- sensor:
- name: "Solar export since start of day"
unique_id: solar_export_since_start_of_day
value_template: >
{{ states('sensor.jenkins_family_solar_export')|float(0) -
states('sensor.solar_export_at_start_of_day')|float(0) }}
unit_of_measurement: 'kWh'
device_class: energy
and when I try to reload the configuration it throws a bunch of errors:
Logger: homeassistant.config
Source: config.py:622
First occurred: 14:52:02 (8 occurrences)
Last logged: 14:52:06
Invalid config for ‘template’ at configuration.yaml, line 51: required key ‘state’ not provided Invalid config for ‘template’ at configuration.yaml, line 53: ‘value_template’ is an invalid option for ‘template’, check: sensor->0->value_template
Invalid config for ‘template’ at configuration.yaml, line 58: required key ‘state’ not provided Invalid config for ‘template’ at configuration.yaml, line 60: ‘value_template’ is an invalid option for ‘template’, check: sensor->0->value_template
Sorry, got mixed up between legacy and modern configuration for template sensors. state not value_template, corrected above.
Be aware that the unique_id doesn’t have any influence on the name or entity ID of the sensor. If you’re providing it to allow for assigning areas etc., I’d recommend using a random UUID via a tool like this.