I keep wrapping my head around this since days and tried various solutions from this forum.
I got several smart outlets measuring energy. Also, I know my home consumes around 200Wh as base consumption. I would like to combine all of that into one thing (virtual sensor? not sure what the correct term is). That thing I want to add on the energy dashboard to get a rough idea of the daily consumption of that 200Wh + all smart outlets.
This is what I got:
template:
- sensor:
- name: "Total_power_consumption_kWh_energy"
unit_of_measurement: kWh
state: >
{% set state1 = states('sensor.fritz_dect_200_5_power_consumption') %}
{% set state2 = states('sensor.fritz_dect_200_3_power_consumption') %}
{% set state3 = states('sensor.0001bb90a452c0_power') %}
{% set state4 = states('sensor.oeq2078902_power') %}
{% if is_number(state1) and is_number(state2) and is_number(state3) and is_number(state4) %}
{{ (( state1|float + state2|float + state3|float + state4|float + 200|float) / 1000) | float | round(2) }}
{% endif %}
device_class: energy
state_class: measurement
attributes:
last_reset: '1970-01-01T00:00:00+00:00'
I was able to add that virtual sensor to the energy dashboard but it shows the wrong values (1.38 kWh right now, should be 0.138 kWh at the most). I am not sure if that is merely a calculation error or I am using a wrong type of state_class? Is this even the corret class at all? Also tried “total” and “total_increasing” but that did not render the correct results either. Where is my mistake?
All of your states are strings. States are always strings. That is why you need the |float or |int filter. And these filters need default values. Only attributes can be other types.
You should be using the state class total_increasing that does not need a last reset.
You should use an availability template to protect against sensors being unavailable.
Taking all that into account:
template:
- sensor:
- name: "Total_power_consumption_kWh_energy"
unit_of_measurement: kWh
state: >
{% set state1 = states('sensor.fritz_dect_200_5_power_consumption')|float(none) %}
{% set state2 = states('sensor.fritz_dect_200_3_power_consumption')|float(none) %}
{% set state3 = states('sensor.0001dd89a452c0_power')|float(none) %}
{% set state4 = states('sensor.oeq1977902_power')|float(none) %}
{{ (( state1 + state2 + state3 + state4 + 200) / 1000) | round(2) }}
device_class: energy
state_class: total_increasing
availability: >
{{ state1 != none and state2 != none and state3 != none and atate4 != none }}
At least that is how you would do it if the state template was not evaluated when the availability was false. Unfortunately that is not (yet) the case. So it will generate errors, if one of the sensors is unknown or unavailable.
So this is how it must be done for now:
template:
- sensor:
- name: "Total_power_consumption_kWh_energy"
unit_of_measurement: kWh
state: >
{% set state1 = states('sensor.fritz_dect_200_5_power_consumption')|float(0) %}
{% set state2 = states('sensor.fritz_dect_200_3_power_consumption')|float(0) %}
{% set state3 = states('sensor.0001dd89a452c0_power')|float(0) %}
{% set state4 = states('sensor.oeq1977902_power')|float(0) %}
{{ (( state1 + state2 + state3 + state4 + 200) / 1000) | round(2) }}
device_class: energy
state_class: total_increasing
availability: >
{{ states('sensor.fritz_dect_200_5_power_consumption')|float(none) != none and
states('sensor.fritz_dect_200_3_power_consumption')|float(none) != none and
states('sensor.0001dd89a452c0_power')|float(none) != none and
states('sensor.oeq1977902_power')|float(none) != none }}
Also a word of warning about the template editor. It interprets result types. So this might say it is a number in the template editor results:
Thank you very much for the detailed information you provided, helping me to get a much better understanding of everything. Thanks to that I adapted my code and after a few days of testing it is now working as expected. Here is the code (state3 and 4 sensors are Homematic IP outlets that measure in Wh, hence divided by 1000):
template:
- sensor:
- name: "Total_power_consumption_kWh"
unit_of_measurement: kWh
state: >
{% set state1 = states('sensor.fritz_dect_200_5_total_energy')|float(0) %}
{% set state2 = states('sensor.fritz_dect_200_3_total_energy')|float(0) %}
{% set state3 = states('sensor.0001bb90a452c0_energy_counter')|float(0) %}
{% set state4 = states('sensor.oeq2078902_energy_counter')|float(0) %}
{{ ( state1 + state2 + ((state3 + state4) / 1000) + 200) | round(2) }}
device_class: energy
state_class: total_increasing
availability: >
{{ states('sensor.fritz_dect_200_5_total_energy')|float(none) != none and
states('sensor.fritz_dect_200_3_total_energy')|float(none) != none and
states('sensor.0001bb90a452c0_energy_counter')|float(none) != none and
states('sensor.oeq2078902_energy_counter')|float(none) != none }}