The sensors are showing the correct value but the dashboard is showing a totally different (nagitiv) value.
I have waited 2 days to see but no improvement.
Yes but you need an availability_template for your template sensor to prevent the |float filter converting the state unavailable to 0. Search the forum. There are many examples.
Yes, thanks, I have now changed the states() to the recommended syntax.
BUT the strange thing to my knowledge is that the ‘rest_energy’ sensor is showing correct when checking the developer tool as shown in the picture. It is just the dashboard that show wrong value…
Because when one of your sensors is unavailable the value of the template decreases. That is taken by the state_class total_increasing to mean your sensor has reset.
Add an availability template and this will solve your issue.
- platform: template
sensors:
rest_energy:
value_template: "{{ (states('sensor.accumulated_consumption') | float - states('sensor.poolpump_today') | float - states('sensor.poolheater_today') | float ) | round(2) }}"
availability_template: "{{ states('sensor.accumulated_consumption') not in ['unknown', 'unavailable'] and states('sensor.poolpump_today') not in ['unknown', 'unavailable'] and states('sensor.poolheater_today') not in ['unknown', 'unavailable'] }}"
unit_of_measurement: "kWh"
Hey @tom_l ,
thanks for that piece of code, I think I am on the good way to make it a little more dynamic
---
platform: template
sensors:
kitchen_total_energy_usage:
friendly_name: Kitchena Daily Energy Usage
entity_id: sensor.time
unit_of_measurement: kWh
icon_template: mdi:counter
availability_template: >
{% set ns = namespace(states=[]) %}
{% for s in states.sensor %}
{% if s.object_id.startswith('kitchen_') and s.object_id.endswith('_energy_total') %}
{% if ( s.state ) not in ['unknown', 'unavailable'] %}
{% if ( ns.state ) not in ['unknown', 'unavailable'] %}
{% set ns.states = "True" %}
{% endif %}
{% else %}
{% set ns.states = "False" %}
{% endif %}
{% endif %}
{% endfor %}
{{ ns.states }}
value_template: >
{% set ns = namespace(states=[]) %}
{% for s in states.sensor %}
{% if s.object_id.startswith('kitchen_') and s.object_id.endswith('_energy_total') %}
{% if ( s.state | float ) > 0.000 %}
{% set ns.states = ns.states + [ states(s.entity_id) | float ] %}
{% endif %}
{% endif %}
{% endfor %}
{{ ns.states | sum | round(2) }}
This way it will include all the plugs/devices based on the search pattern, so that I can group per-room (or do the whole house) without having to specify each single plug/device.
The problem is that I don’t really know what else to do as it seems that the instability of the device causes the sensor to miss-report even if the availability template is there…
Sorry, not sure I understand what you mean.
The value of the template needs to result 0 if the device is not available?
Also, the code you paseted seems to be the same as mine, is that correct or I don’t see something in there?
Thanks for taking the time to follow my madness, as usual, much appreciated
Maybe the availability_template it’s actually still needed, or how could it deal with multiple device in that loop?
For what I understand, that will work for the 1 device that is set to “0”, but if all other are correct, I might potentially have a drop and then an increase again when the offline device comes back, is that wrong?