Template Trend Sensor not working

I want to build a sensor with HA that can detect spikes of my Lyric T6 thermostat. See below examples op these spikes (at 05:27h and 08:03h):

I use a so-called Trend Sensor (Trend - Home Assistant) to detect the following deviation:

  • a deviation of more than 0.4 degrees
  • in 5 minutes
  • over 60 minutes

If I understand the documentation correctly, I should use the following values in the sensor:

sample_duration: 3600 (=60 * 60)
max_samples: 60
min_samples: 10
min_gradient: 0.001333333 (=0.4 / (60 * 5)

This then leads to the following sensor:

binary_sensor:
  - platform: trend
    sensors:
      t6_spikes:
        entity_id: sensor.heatpump_thermostat_room_temperature
        sample_duration: 3600
        max_samples: 60
        min_samples: 10
        min_gradient: 0.001333333

The result is always: unknown, so no detection was done at 05:27h and 08:03h…

What am I missing/doing wrong??

Have you waited long enough?

This sensor requires at least two updates of the underlying sensor to establish a trend. Thus it can take some time to show an accurate state.

If the sensor state stays static for a long time, that’s not an “update”.

You could make a “noisy” template sensor that updates every minute:

template:
  - trigger:
      - platform: time_pattern
        minutes: "/1"
    sensor:
      - name: Noisy thermostat
        state: "{{ states('sensor.heatpump_thermostat_room_temperature')|float(0) + (range(100)|random / 10000) }}"
        unit_of_measurement: "°C"

Will give that a test. Adjusted the Trend sensor to monitor your “noisy” template sensor, with a small adjustment for the random value (as yours would never create a spike above 0,4), so using (range(1000)|random / 1000) .

- platform: trend
  sensors:
    t6_test:
      entity_id: sensor.noisy_thermostat
      sample_duration: 3600
      max_samples: 60
      min_samples: 10
      min_gradient: 0.001333333

Then you misunderstand the need for the noise. It shouldn’t generate spikes, just near-insignificant changes to the value to force a state update every minute.

Okay, reverted :innocent:
I understand now why I need a “noisy” template sensor. This is to have enough samples in the Trend Sensor for that sensor to check the trend. Thx!

Your min samples is killing you. Over 8 hours I see 9 state changes, I.e. 9 samples. It will never get to the sample rate that you have as a minimum.

FYI, the T6 also doesn’t really have the resolution for temperature that you’re looking to detect. Try using the derivative sensor instead. Which will provide you with the rate of change of temperature.

1 Like

Thx, so removing the min_samples line would do the trick? Or lowering it? In that case to what value do you propose?

the derivative integration.

@petro , just to understand what you mean.

Do you want me to create a Derivative Sensor based on my Room Thermostat and use that Derivative Sensor to check with a Trend Sensor?

So, a Derivative Sensor looking at my Room Temperature:

sensor:
  - platform: derivative
    name: Derivative Room Temperature
    source: sensor.heatpump_thermostat_room_temperature

and next the Trend Sensor checking the Derivative Sensor:

- platform: trend
  sensors:
    t6_spikes:
      entity_id: sensor.derivative_room_temperature
      sample_duration: 300
      max_samples: 5
      min_gradient: 0.0033333

Is this what you mean?

No, stop using trend. Just use derivative. Watch the value for a few days and you’ll then know what numerical value to trigger on with your automation.

There isn’t going to be a magic bullet that you can provide right out of the gate.