Add stability to automations comparing temperatures

I have fan which ventilates my garage and uses an outside temperature sensor and a temperature sensor in the garage. When it’s cooler outside than in the garage, the fan turns on and vice-versa. It’s been working great except there’s a minute or two when the threshold is crossed mid-morning and evening where it turns the fan off and on again while the temps are just across the threshold and then back. What’s the best way to add stability so that it doesn’t do this? I’ve tried adding a delay as the first step in the action, but that didn’t seem to help.

- id: '1689129656297'
  alias: Garage Vent On Summer
  description: When the Garage is hotter than the outside temp the vent
    turns on
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.garage_temperature'')|float > (states(''sensor.outside_temperature'')|float)
      }}'
  condition: []
  action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.garage_ventilation_fan
  - service: notify.mobile_app_tylers_iphone
    data:
      message: Stopping Garage Ventilation
  mode: single
- id: '1689175984298'
  alias: Garage Vent Off Summer
  description: When the Garage is cooler than the outside temp the vent
    turns off
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.garage_temperature'')|float < (states(''sensor.outside_temperature'')|float)
      }}'
  condition: []
  action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.garage_ventilation_fan
  - service: notify.mobile_app_tylers_iphone
    data:
      message: Starting Garage Ventilation
  mode: single

Hi, I think you might get better results using a generic thermostat as this includes tolerances (i.e. deadbands) which should get around the problem that you are seeing. You would just need to create a template sensor for the difference between the garage & outside temperature and then use that as the target_sensor of the thermostat. If you set the target_temp to 5.0 then the fan would start when the garage is 5 degrees hotter than outside.

The following is an example from my config for controlling the fans in a server cabinet:

- platform: generic_thermostat
  name: Server Cabinet
  heater: switch.sonoff03
  target_sensor: sensor.server_cabinet_temperature
  min_temp: 20
  max_temp: 35
  ac_mode: true
  target_temp: 30.0
  cold_tolerance: 0.5
  hot_tolerance: 0.5
  min_cycle_duration:
    seconds: 10
  initial_hvac_mode: "cool"
  precision: 0.1

Another option is to assign a minimum duration to the trigger by using the for key:

trigger:
  - platform: template
    value_template: |-
      {{ states('sensor.garage_temperature')|float > states('sensor.outside_temperature')|float }}
    for: "00:03:00"

That will require the difference in temperature to be stable before actions are taken. You can add a similar duration to your “off” automation, but it will likely be fine just having it on one of them.

A duration added to the “turn on” automation will create a minimum rest period for the fan, while one added to the “turn off” automation will act as a minimum run time.

That’s super simple and straightforward. I’ll give it a try.

That seemed to work great this morning. I only got 1 notification that the fan was turned off. Thanks!

1 Like