I don’t know why the code I wrote does not work, but this one works:
template:
- trigger:
- platform: time_pattern
# You can also match on interval. This will match every 1 minutes
minutes: "/1"
sensor:
- name: "test"
state: >
{% if states('sensor.test') | int(default=0) == 0 %}
{% set acum = 1 %}
{% else %}
{% set acum = states('sensor.test') | int(default=0) + 1 %}
{% endif %}
{{ acum }}
So, the problem seems related to updating the sensor.
“entity_id: xxx” has been deprecated for sensors a long time ago.
I’m surprised you didn’t get a notification in your logs about it. But then again maybe its been so long that they don’t even give the warning anymore.
the only way a sensor gets updated is if one of the referenced entities changes state and since the only entity you referenced is itself then it never gets updated.
you could have added the following to the template and it should update every minute (I think…):
value_template: >
{% set update = states.('sensor.time') %}
{% if states('sensor.test') | int(default=0) == 0 %}
{% set acum = 1 %}
{% else %}
{% set acum = states('sensor.test') | int(default=0) + 1 %}
{% endif %}
{{ acum }}
I’d like to create a sensor with the exponentially descreasing weighted average of another sensor. The problem I see is that new value of the sensor will depend on previous value. If HA is restarted, sensor value with the historic evolution is lost. I suppose this can be avoided by saving the sensor’s actual state in an input_number.