Statistics sensor for power with template source erroring

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.

Mission accomplished. :man_shrugging:

By not returning a value, the Statistics sensor receives nothing and the result is:

parsing error, expected number and received

I suggest you use availability for that purpose, instead of a state template that doesn’t return a value.

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?

No, that’s not necessary and I don’t know what in the documentation led you to conclude it’s required.

Try this and see if it helps:

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 }}

EDIT

Correction. Replaced trailing %} with }}.

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?

Thanks.

Check the history for sensor.total_power (or in Logbook), focus on the value it reports on startup (when the error message is produced).

I checked the database while restarting Home Assistant and do see that sensor.total_power does return unknown briefly while its starting.

Is there a way to tell the statistics sensor to not process a value when the source data is unknown?

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 %}