-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfan-controller-nuc.yaml
221 lines (203 loc) · 6.36 KB
/
fan-controller-nuc.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
substitutions:
device_name: "Fan Controller NUC"
espname: "fan-controller-nuc"
esphome:
name: '${espname}'
friendly_name: ${device_name}
comment: 'ESP32S2'
platformio_options:
board_build.f_flash: 40000000L
board_build.flash_mode: dio
board_build.flash_size: 4MB
board_build.extra_flags:
- "-DARDUINO_USB_CDC_ON_BOOT=0" # Override, defaults to '-DARDUINO_USB_CDC_ON_BOOT=1'
on_boot:
priority: -100
then:
- fan.turn_on:
id: speedfan
speed: 60
esp32:
board: lolin_s2_mini
variant: ESP32S2
framework:
type: esp-idf
packages:
wifi: !include common/wifi.yaml
network_diagnostics: !include common/network_diagnostics.yaml
base_config: !include common/base_config.yaml
wifi:
use_address: 10.0.30.111
status_led:
pin: GPIO15
sensor:
- platform: homeassistant
id: nuc_cpu_temperature
entity_id: sensor.processor_temperature
internal: true
filters:
- heartbeat: 5s
- sliding_window_moving_average:
window_size: 60
send_every: 60
- platform: homeassistant
id: nuc_nvme_temperature
entity_id: sensor.nuc_nvme_composite_temperature
internal: true
filters:
- heartbeat: 5s
- sliding_window_moving_average:
window_size: 60
send_every: 60
- platform: pulse_counter
name: "NUC External Fan Speed"
pin:
number: GPIO09
mode: INPUT_PULLUP
unit_of_measurement: 'RPM'
update_interval: 1s
accuracy_decimals: 0
id: fanspeed_sensor
filters:
- multiply: 0.5
- or:
- throttle_average: 60s
- delta: 100
output:
# Wire this pin (07) into the PWM pin of your 5v fan
# ledc is the name of the pwm output system on an esp32
- platform: ledc
id: fan_output
pin: GPIO07
# 25KHz is standard PC fan frequency, minimises buzzing
frequency: "25000 Hz"
min_power: 1%
max_power: 100%
fan:
- platform: speed
output: fan_output
id: speedfan
name: "NUC External Fan"
restore_mode: RESTORE_DEFAULT_ON
number:
- platform: template
name: "NUC Fan Setting - NVMe Min Temperature"
id: nvme_min_temp
unit_of_measurement: "°C"
icon: "mdi:thermometer-low"
restore_value: true
initial_value: 30
min_value: 20
max_value: 40
step: 1
optimistic: true
- platform: template
name: "NUC Fan Setting - NVMe Max Temperature"
id: nvme_max_temp
unit_of_measurement: "°C"
icon: "mdi:thermometer-high"
restore_value: true
initial_value: 70
min_value: 41
max_value: 80
step: 1
optimistic: true
- platform: template
name: "NUC Fan Setting - CPU Min Temperature"
id: cpu_min_temp
unit_of_measurement: "°C"
icon: "mdi:thermometer-low"
restore_value: true
initial_value: 30
min_value: 20
max_value: 40
step: 1
optimistic: true
- platform: template
name: "NUC Fan Setting - CPU Max Temperature"
id: cpu_max_temp
unit_of_measurement: "°C"
icon: "mdi:thermometer-high"
restore_value: true
initial_value: 80
min_value: 41
max_value: 80
step: 1
optimistic: true
- platform: template
name: "NUC Fan Setting - Min Fan Speed"
id: min_fan_speed
unit_of_measurement: "%"
icon: "mdi:fan-minus"
restore_value: true
initial_value: 20
min_value: 0
max_value: 40
step: 1
optimistic: true
- platform: template
name: "NUC Fan Setting - Max Fan Speed"
id: max_fan_speed
unit_of_measurement: "%"
icon: "mdi:fan-plus"
restore_value: true
initial_value: 80
min_value: 41
max_value: 100
step: 1
optimistic: true
interval:
- interval: 10s
then:
- lambda: |-
// Read temperatures
bool cpu_available = !isnan(id(nuc_cpu_temperature).state);
bool nvme_available = !isnan(id(nuc_nvme_temperature).state);
float cpu_temp = cpu_available ? id(nuc_cpu_temperature).state : 0;
float nvme_temp = nvme_available ? id(nuc_nvme_temperature).state : 0;
// Get user-defined input values
float nvme_min = id(nvme_min_temp).state;
float nvme_max = id(nvme_max_temp).state;
float cpu_min = id(cpu_min_temp).state;
float cpu_max = id(cpu_max_temp).state;
float min_speed = id(min_fan_speed).state;
float max_speed = id(max_fan_speed).state;
// Debug input values
ESP_LOGD("fan_control", "NVMe Min: %.1f, NVMe Max: %.1f", nvme_min, nvme_max);
ESP_LOGD("fan_control", "CPU Min: %.1f, CPU Max: %.1f", cpu_min, cpu_max);
ESP_LOGD("fan_control", "Min Fan Speed: %.1f, Max Fan Speed: %.1f", min_speed, max_speed);
// Ensure valid ranges
if (nvme_max <= nvme_min) {
ESP_LOGW("fan_control", "Invalid NVMe range: Min (%.1f) >= Max (%.1f)", nvme_min, nvme_max);
return;
}
if (cpu_max <= cpu_min) {
ESP_LOGW("fan_control", "Invalid CPU range: Min (%.1f) >= Max (%.1f)", cpu_min, cpu_max);
return;
}
if (max_speed <= min_speed) {
ESP_LOGW("fan_control", "Invalid Fan Speed range: Min (%.1f) >= Max (%.1f)", min_speed, max_speed);
return;
}
// Calculate fan speeds
float nvme_speed = nvme_available
? ((nvme_temp - nvme_min) / (nvme_max - nvme_min)) * (max_speed - min_speed) + min_speed
: min_speed;
float cpu_speed = cpu_available
? ((cpu_temp - cpu_min) / (cpu_max - cpu_min)) * (max_speed - min_speed) + min_speed
: min_speed;
// Clamp values between min and max fan speed
nvme_speed = std::clamp(nvme_speed, min_speed, max_speed);
cpu_speed = std::clamp(cpu_speed, min_speed, max_speed);
// Use the maximum of the two calculated speeds
float final_speed = cpu_available || nvme_available
? std::max(nvme_speed, cpu_speed)
: 60; // Default to 60% if sensors are unavailable
// Log the calculated speeds
ESP_LOGI("fan_control",
"CPU Temp: %.1f, NVMe Temp: %.1f, NVMe Speed: %.1f%%, CPU Speed: %.1f%%, Final Speed: %.1f%%",
cpu_temp, nvme_temp, nvme_speed, cpu_speed, final_speed);
// Set fan speed
auto call = id(speedfan).turn_on();
call.set_speed(final_speed);
call.perform();