I’ve created a statistics sensor for monitoring some of my power usage that gets its data from a template sensor which gets its data from CT clamps from ESPHome. However when using the senor, or when restarting HASS, I get the following in the logs many times:
2021-09-12 13:52:02 ERROR (MainThread) [homeassistant.components.statistics.sensor] sensor.weekly_power_stats: parsing error, expected number and received
2021-09-12 13:52:02 ERROR (MainThread) [homeassistant.components.statistics.sensor] sensor.weekly_power_stats: parsing error, expected number and received
2021-09-12 13:52:02 ERROR (MainThread) [homeassistant.components.statistics.sensor] sensor.weekly_power_stats: parsing error, expected number and received
2021-09-12 13:52:02 ERROR (MainThread) [homeassistant.components.statistics.sensor] sensor.weekly_power_stats: parsing error, expected number and received
2021-09-12 13:52:02 ERROR (MainThread) [homeassistant.components.statistics.sensor] sensor.weekly_power_stats: parsing error, expected number and received
2021-09-12 13:52:02 ERROR (MainThread) [homeassistant.components.statistics.sensor] sensor.weekly_power_stats: parsing error, expected number and received
2021-09-12 13:52:02 ERROR (MainThread) [homeassistant.components.statistics.sensor] sensor.weekly_power_stats: parsing error, expected number and received
2021-09-12 13:52:02 ERROR (MainThread) [homeassistant.components.statistics.sensor] sensor.weekly_power_stats: parsing error, expected number and received
relevant config:
sensor:
- platform: statistics
entity_id: sensor.total_power
name: "Weekly Power Stats"
max_age:
days: 7
sampling_size: 42000 # 6k * 7
template:
sensor:
- name: "Total Power"
unit_of_measurement: "W"
device_class: power
state: >
{% set A = states('sensor.a_power') | int %}
{% set B = states('sensor.b_power') | int %}
{% if (A + B) > 0 -%}
{{ (A + B) }}
{%- endif %}
In this config sensor.a_power and sensor.B_power are the raw values from the ESPHome CT camps (converted to Watts)
I’m guessing that the error is being thrown when the condition in the template condition is not true. Is there a better way to handle this? I added the >0 condition to help filter out invalid or incorrect values from ESPHome which can occur when ESPHome takes a measurement while it is still booting, and in an attempt to not return values if either of the sensors are unavailable.
Thanks for the pointer to the availability template option. That sounds like the correct solution.
After reading the documentation, it seems that I need to convert my state template sensor to a sensor trigger template. But then I need to manually define when to update/trigger and the template can’t automatically update its value when its dependents are updated automatically?
Is there not a simple way to tell a template sensor that it should be unavailable if its dependents are unavailable?
template:
sensor:
- name: "Total Power"
unit_of_measurement: "W"
device_class: power
state: "{{ states('sensor.a_power') | int + states('sensor.b_power') | int }}"
availability: >
{% set x = ['unknown', 'unavailable'] %}
{{ not states('sensor.a_power') in x and not states('sensor.a_power') in x }}
I misread part of the template documentation, I see now how it works with availability.
Thanks for providing an example. I just gave it a try (after fixing a syntax error replacing %} with }}) but I still get the same error in the Home Assistant logs.
Is there a way to get more information on the log to see if something else might be the problem?
I was able to solve this. The problem is that the statistics sensor does not check the source sensor’s availability. Instead it looks for the sensor state to be unavailable or unknown. So changing my template to the following fixed it:
template:
sensor:
- name: "Total Power"
unit_of_measurement: "W"
device_class: power
state: >
{% set A = states('sensor.a_power') | int %}
{% set B = states('sensor.b_power') | int %}
{% if (A + B) > 0 -%}
{{ (A + B) }}
{% else %}
unavailable
{%- endif %}