Skip to content

Instantly share code, notes, and snippets.

@kepstin
Last active May 13, 2024 17:23
Show Gist options
  • Save kepstin/7eb43eeabf97348a256fb33fd4d85a57 to your computer and use it in GitHub Desktop.
Save kepstin/7eb43eeabf97348a256fb33fd4d85a57 to your computer and use it in GitHub Desktop.
Home Assistant EWMA template sensor
- trigger:
- id: state_changed
platform: state
entity_id: sensor.147334558305449_indoor_temperature
to: null
- id: time_pattern
platform: time_pattern
seconds: "/30"
action:
- variables:
tau: 300
precision: 1
sensor_name: 147334558305449_indoor_temperature
entity_id: sensor.147334558305449_indoor_temperature
sensor:
- name: "Heat Pump Smoothed Indoor Temperature"
unique_id: b414f387d861985d8849c56a1b263317
icon: mdi:home-thermometer
availability: "{{ states(entity_id) | is_number }}"
device_class: temperature
state: >-
{% set last_state = this.state | float(states(entity_id) | float) %}
{% set last_state_changed = this.last_changed
| as_timestamp(states.sensor[sensor_name].last_changed | as_timestamp) %}
{% set next_state = trigger.from_state.state
| float(states(entity_id) | float)
if trigger.id == 'state_changed'
else states(entity_id) | float %}
{% set next_state_changed = now() | as_timestamp %}
{% set dt = next_state_changed - last_state_changed %}
{% set w = e ** (-dt / tau) %}
{% set state = last_state * w + next_state * (1-w) %}
{% if trigger.id == 'state_changed' or state | round(precision) != last_state | round(precision) %}{{ state }}{% else %}{{ last_state }}{% endif %}
unit_of_measurement: "°C"
state_class: measurement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment