EDIT: I started with below strike through question. But it turned out to better use a statistics sensor. Thus below code is not used and this post is now going further about using a statistic sensor.
As I am reading through my logging (once in a while it’s cleanup time) I stumble upon one of my favorite topics - template sensors.
I find 3 errors in my log and they refer to 3 template sensors. I am not sure how the ended up in my config, but most probably I added them . I think I was playing with an accumulated sensor to count the water amount per every last 5 minutes.
in short: they are all “unavailable”. Probably because there is an error (again) in the syntax of them… And there I need some help (please be gentle with me Petro) to find what is causing the error and how I can get such a sensor. Below probably 3 tries:
The sensor config:
</s> <s> - platform: template</s> <s> sensors:</s> <s> accumulated_water_usage_5min:</s> <s> friendly_name: "Accumulated Water Usage (5 min)"</s> <s> value_template: ></s> <s> {% set window_start = now() - timedelta(minutes=5) %}</s> <s> {% set water_consumption = states('sensor.water_consumptie_nu') %}</s> <s> {% set events = state_changes['sensor.water_consumptie_nu'] %}</s> <s> {% set accumulated_usage = 0 %}</s> <s> {% for event in events %}</s> <s> {% if event.state == 'unknown' %}</s> <s> {% set accumulated_usage = 0 %}</s> <s> {% elif event.state | float > 0 and event.last_changed > window_start %}</s> <s> {% set accumulated_usage = accumulated_usage + (event.state | float) %}</s> <s> {% endif %}</s> <s> {% endfor %}</s> <s> {{ accumulated_usage | round(2) }}</s> <s> </s> <s> - platform: template</s> <s> sensors:</s> <s> accumulated_water_usage_5min_2:</s> <s> friendly_name: "Accumulated Water Usage (5 min) 2"</s> <s> unit_of_measurement: "liters" # Adjust as needed</s> <s> value_template: ></s> <s> {% set window_start = now() - timedelta(minutes=5) %}</s> <s> {% set accumulated_usage = 0 %}</s> <s> {% for event in state_attr('sensor.water_consumptie_nu', 'all') %}</s> <s> {% if event.state != 'unknown' and event.last_changed > window_start %}</s> <s> {% set accumulated_usage = accumulated_usage + (event.state | float) %}</s> <s> {% endif %}</s> <s> {% endfor %}</s> <s> {{ accumulated_usage | round(2) }}</s> <s> </s> <s> - platform: template</s> <s> sensors:</s> <s> accumulated_water_usage_5min_3:</s> <s> friendly_name: "Accumulated Water Usage (5 min) 3"</s> <s> unit_of_measurement: "liters" # Adjust as needed</s> <s> value_template: ></s> <s> {% set interval = 5 * 60 %} # 5 minutes in seconds</s> <s> {% set time_threshold = now() - timedelta(seconds=interval) %}</s> <s> {% set accumulated_usage = 0 %}</s> <s> {% for event in state_attr('sensor.water_consumptie_nu', 'all') %}</s> <s> {% if event.state != 'unknown' and event.last_changed >= time_threshold %}</s> <s> {% set accumulated_usage = accumulated_usage + (event.state | float) %}</s> <s> {% endif %}</s> <s> {% endfor %}</s> <s> {{ accumulated_usage | round(2) }}</s> <s>
The error:
```
Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:194
Integration: Template (documentation, issues)
First occurred: 19:47:47 (3 occurrences)
Last logged: 19:47:47
TemplateError(‘UndefinedError: ‘state_changes’ is undefined’) while processing template ‘Template<template=({% set window_start = now() - timedelta(minutes=5) %} {% set water_consumption = states(‘sensor.water_consumptie_nu’) %} {% set events = state_changes[‘sensor.water_consumptie_nu’] %} {% set accumulated_usage = 0 %} {% for event in events %} {% if event.state == ‘unknown’ %} {% set accumulated_usage = 0 %} {% elif event.state | float > 0 and event.last_changed > window_start %} {% set accumulated_usage = accumulated_usage + (event.state | float) %} {% endif %} {% endfor %} {{ accumulated_usage | round(2) }}) renders=4>’ for attribute ‘_attr_native_value’ in entity ‘sensor.accumulated_water_usage_5min’
TemplateError(‘TypeError: ‘NoneType’ object is not iterable’) while processing template ‘Template<template=({% set window_start = now() - timedelta(minutes=5) %} {% set accumulated_usage = 0 %} {% for event in state_attr(‘sensor.water_consumptie_nu’, ‘all’) %} {% if event.state != ‘unknown’ and event.last_changed > window_start %} {% set accumulated_usage = accumulated_usage + (event.state | float) %} {% endif %} {% endfor %} {{ accumulated_usage | round(2) }}) renders=4>’ for attribute ‘_attr_native_value’ in entity ‘sensor.accumulated_water_usage_5min_2’
TemplateError(‘TypeError: ‘NoneType’ object is not iterable’) while processing template ‘Template<template=({% set interval = 5 * 60 %} # 5 minutes in seconds {% set time_threshold = now() - timedelta(seconds=interval) %} {% set accumulated_usage = 0 %} {% for event in state_attr(‘sensor.water_consumptie_nu’, ‘all’) %} {% if event.state != ‘unknown’ and event.last_changed >= time_threshold %} {% set accumulated_usage = accumulated_usage + (event.state | float) %} {% endif %} {% endfor %} {{ accumulated_usage | round(2) }}) renders=4>’ for attribute ‘_attr_native_value’ in entity ‘sensor.accumulated_water_usage_5min_3’
```