Skip to content

Instantly share code, notes, and snippets.

@pantherale0
Last active July 25, 2024 13:48
Show Gist options
  • Save pantherale0/24e5e64cfb53d3cd86b181c5866594ca to your computer and use it in GitHub Desktop.
Save pantherale0/24e5e64cfb53d3cd86b181c5866594ca to your computer and use it in GitHub Desktop.
HA: Auto Fan Speed v2
mode: restart
blueprint:
name: Auto fan speed
description: "Temperature based Auto fan control.\n\n Fan Speed will be set when
initially turned on by relating the ambient temperature to an equivalant speed
setting. \nA time delay and a minimum percentage change is used to eliminate
frequent speed changes.\n At the minimum temperature setting the fan will turn
off. \n When the temperature rises above this minimum temperature setting, the
fan will automatically turn back on."
domain: automation
input:
temp_sensor:
name: 🌡 Temperature Sensor
description: Enter your temperature sensor. Make sure the temperature sensor is in the units (°C or °F) you want to use for the settings below.
If your temp sensor is in °C but you want to use the °F settings, you will need to create a template sensor to convert the sensor into the correct units.
default: []
selector:
entity:
domain:
- sensor
device_class:
- temperature
multiple: false
fan_switch:
name: 💨 Fan
description: The fan you wish to speed control.
selector:
entity:
domain:
- fan
multiple: false
min_fan_speed:
name: 💨 Minimum Fan Speed
description: Set the minimum percentage speed when your fan is still on.
default: 16
selector:
number:
min: 1.0
max: 100.0
mode: slider
step: 1.0
unit_of_measurement: '%'
max_fan_speed:
name: 💨 Maximum Fan Speed
description: Set the maximum percentage speed for your fan.
default: 100
selector:
number:
min: 1.0
max: 100.0
mode: slider
step: 1.0
unit_of_measurement: '%'
max_temp:
name: 📈 Maximum Temperature
description: What temperature would you like the fan to run at max fan speed.
selector:
entity:
domain:
- sensor
- input_number
multiple: false
min_temp:
name: 📉 Minimum Temperature
description: What temperature would you like the fan to run at minimum speed.
selector:
entity:
domain:
- sensor
- input_number
multiple: false
off_temp:
name: 🛑 The temperature the fan switches off
description: What temperature would you like the fan to turn off? When the temperature falls below this value,
the fan turns off. When the temperature rises above this value, the fan will automatically turn back on
(unless you disable this via the "Enable auto fan on" setting).
selector:
entity:
domain:
- sensor
- input_number
multiple: false
auto_turn_on_enabled:
name: ✅ Enable auto fan on
description: Let the fan automatically turn back on if the temperature returns to a value above the temperature that the fan switches off
default: true
selector:
boolean: {}
change_time:
name: ⏱️ Change frequency delay
description: How long to delay bewteen potential speed adjustments.
default: 30
selector:
number:
min: 1.0
max: 120.0
unit_of_measurement: minutes
step: 1.0
mode: slider
change_threshold:
name: Minimum percentage change
description: The minimum percentage change (between current fan speed and set
fan speed)
default: 1
selector:
number:
min: 1.0
max: 100.0
mode: slider
step: 1.0
unit_of_measurement: '%'
max_change:
name: Maximum percentage change
description: The maximum percentage change (between current fan speed and set
fan speed)
default: 50
selector:
number:
min: 1.0
max: 100.0
mode: slider
step: 1.0
unit_of_measurement: '%'
blocker_entity:
name: (OPTIONAL) Blocking entity
description: If this entity's state is on, it will prevent the automation from running. E.g. sleepmode or away mode.
default:
selector:
entity:
multiple: false
source_url: https://community.home-assistant.io/t/adaptive-fan-speed-control-based-on-temperature-and-speed-range/678152
variables:
temp_sensor: !input temp_sensor
fan_switch: !input fan_switch
max_temp_entity: !input max_temp
min_temp_entity: !input min_temp
off_temp_entity: !input off_temp
auto_turn_on_enabled: !input 'auto_turn_on_enabled'
min_fan_speed: !input min_fan_speed
max_fan_speed: !input max_fan_speed
max_temp: "{{ states(max_temp_entity) | float(40) }}"
min_temp: "{{ states(min_temp_entity) | float(23) }}"
off_temp: "{{ states(off_temp_entity) | float(22) }}"
change_time: !input change_time
max_change: !input max_change
change_threshold: !input change_threshold
blocker_entity: !input blocker_entity
current_temp: "{{ states(temp_sensor) | float(0)}}"
fan_speed: '{{ state_attr(fan_switch,''percentage'') | float(0)}}'
temp_range: '{{max_temp | float(0)-min_temp | float(0)}}'
fan_range: '{{max_fan_speed | float(0) - min_fan_speed | float(0)}}'
slope: '{{fan_range | float(0)/temp_range | float(0)}}'
initial_set_fan_speed: '{{ [[slope|float(0) * (current_temp|float(0) - min_temp|float(0)) + min_fan_speed|float(0), min_fan_speed] | max, max_fan_speed] | min}}'
adjusted_set_fan_speed: >
{% set diff = initial_set_fan_speed - fan_speed %}
{% if diff > max_change|float(0) %}
{{ fan_speed + max_change|float(0) }}
{% elif diff < -max_change|float(0) %}
{{ fan_speed - max_change|float(0) }}
{% else %}
{{ initial_set_fan_speed }}
{% endif %}
set_fan_speed: '{{ adjusted_set_fan_speed }}'
speed_diff: '{{(fan_speed - set_fan_speed)|abs}}'
trigger:
- platform: state
entity_id:
- !input fan_switch
id: fanon
from: 'off'
to: 'on'
- platform: state
id: temp_state_change
entity_id:
- !input temp_sensor
- !input max_temp
- !input min_temp
- !input off_temp
condition:
- condition: template
alias: Check for blocker entity
value_template: '{{ (blocker_entity == none) or (states(blocker_entity) == ''off'')}}'
action:
- choose:
- conditions:
- condition: trigger
alias: Only run if fan was switched on
id: fanon
- condition: template
alias: Is the percentage change great enough?
value_template: '{{ speed_diff | float(0) > change_threshold | float(0) }}'
sequence:
- service: homeassistant.turn_on
data:
percentage: '{{set_fan_speed}}'
target:
entity_id: '{{fan_switch}}'
- delay:
minutes: !input change_time
- conditions:
- condition: template
value_template: '{{states(fan_switch) == ''on''}}'
alias: Make sure the fan is already on (ignore if it's been switched off)
- condition: template
alias: Is the percentage change great enough?
value_template: '{{ speed_diff | float(0) > change_threshold | float(0) }}'
- condition: template
alias: Is the temperature above the off temp?
value_template: '{{ current_temp | float(0) > off_temp | float(0) }}'
sequence:
- service: homeassistant.turn_on
target:
entity_id: '{{fan_switch}}'
data:
percentage: '{{set_fan_speed}}'
- delay:
minutes: !input change_time
- conditions:
- condition: not
alias: Make sure the fan hasn't just been turned on
conditions:
- condition: trigger
id: fanon
- condition: numeric_state
entity_id: !input temp_sensor
below: !input off_temp
sequence:
- service: homeassistant.turn_off
target:
entity_id: '{{fan_switch}}'
- conditions:
- '{{ auto_turn_on_enabled }}'
- condition: template
value_template: '{{states(fan_switch) == ''off''}}'
alias: Is the fan currently off?
- condition: template
alias: Is the temperature above the off temp?
value_template: '{{ current_temp | float(0) > off_temp | float(0) }}'
sequence:
- service: homeassistant.turn_on
data:
percentage: '{{set_fan_speed}}'
target:
entity_id: '{{fan_switch}}'
- delay:
minutes: !input change_time
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment