Open shirou93 opened 1 year ago
@shirou93 I think the pulse_width
sensor is sometimes returns 321 or higher instead of 320 when at max fan speed. This would cause the default case to which means fan speed 0.
You could try changing the max
filter to the median
or one of other average filters, along with adjusting the window_size
/send_every
settings.
And below is an alternate lambda code that allows values a range of values instead exact values which might be more reliable, and you can also adjust the buffer
value if it's reading.
@@ -31,17 +31,18 @@
filters:
- multiply: 1000000
- lambda: !lambda |-
- switch (int(x)) {
- case 20:
- return 1;
- case 100:
- return 2;
- case 320:
- return 3;
- default:
- return 0;
+ int buffer = 2;
+ int y = int(x);
+ if (y > (20-buffer) && y < (20+buffer)) {
+ return 1;
+ } else if (y > (100-buffer) && y < (100+buffer)) {
+ return 2;
+ } else if (y > (320-buffer) && y < (320+buffer)) {
+ return 3;
+ } else {
+ return 0;
}
- - max:
+ - median:
window_size: 3
send_every: 3
send_first_at: 1
And if that's not working can add some debug logging so that it prints the y
value which can help understand what values the sensor is reading:
int y = int(x);
+ ESP_LOGD("custom", "led1_pulse_width: %d", y);
if (y > (20-buffer) && y < (20+buffer)) {
@jonathonlui
i have error on compiling:
I try wih your code, and create this one:
- lambda: !lambda |-
int poff = 0;
int y = int(x);
if (y >= 15 and y <= 35) {
return 1;
} else if (y >= 95 and y <= 105) {
return 2;
} else if (y >= 315 and y <= 325) {
return 3;
} else {
return 0;
}
Sometimes state 0 not working
I try wih your code, and create this one:
- lambda: !lambda |- int poff = 0; int y = int(x); if (y >= 15 and y <= 35) { return 1; } else if (y >= 95 and y <= 105) { return 2; } else if (y >= 315 and y <= 325) { return 3; } else { return 0; }
Sometimes state 0 not working
This seems to work, thanks. I expanded the margins a bit:
filters:
# The values after multiplied by 1000000 are 0, 20, 100, 320
# and correspond to LED being off, low, med, high intensity.
# The "pulse_width" sensor sometimes reads non-0 (e.g 221) when
# the LED is off which is why 0 (off state) is returned in the
# default case.
- multiply: 1000000
- lambda: !lambda |-
int poff = 0;
int y = int(x);
if (y >= 10 and y <= 40) {
return 1;
} else if (y >= 90 and y <= 110) {
return 2;
} else if (y >= 310 and y <= 330) {
return 3;
} else {
return 0;
}
- median:
window_size: 3
send_every: 3
send_first_at: 1
- or:
- delta: 0.5
- throttle: 60s
Here's a sample of my LED outputs.
[12:02:21][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:21][D][custom:074]: led1_pulse_width: 0
[12:02:21][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:21][D][custom:074]: led1_pulse_width: 0
[12:02:22][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:22][D][custom:074]: led1_pulse_width: 0
[12:02:22][D][button:010]: 'Power Button' Pressed.
[12:02:22][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:22][D][custom:074]: led1_pulse_width: 12
[12:02:22][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:22][D][custom:074]: led1_pulse_width: 12
[12:02:22][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:22][D][custom:074]: led1_pulse_width: 12
[12:02:23][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:23][D][custom:074]: led1_pulse_width: 28
[12:02:23][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:23][D][custom:074]: led1_pulse_width: 20
[12:02:23][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:23][D][custom:074]: led1_pulse_width: 29
[12:02:23][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:23][D][custom:074]: led1_pulse_width: 28
[12:02:24][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:24][D][custom:074]: led1_pulse_width: 28
[12:02:24][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:24][D][custom:074]: led1_pulse_width: 19
[12:02:24][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:24][D][custom:074]: led1_pulse_width: 21
[12:02:24][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:24][D][custom:074]: led1_pulse_width: 12
[12:02:24][D][sensor:094]: 'Fan Speed': Sending state 1.00000 with 0 decimals of accuracy
[12:02:25][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:25][D][custom:074]: led1_pulse_width: 24
[12:02:25][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:25][D][custom:074]: led1_pulse_width: 11
[12:02:25][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:25][D][custom:074]: led1_pulse_width: 12
[12:02:25][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:25][D][custom:074]: led1_pulse_width: 24
[12:02:26][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:26][D][custom:074]: led1_pulse_width: 18
[12:02:26][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:26][D][custom:074]: led1_pulse_width: 12
[12:02:26][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:26][D][custom:074]: led1_pulse_width: 18
[12:02:26][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:26][D][custom:074]: led1_pulse_width: 24
[12:02:27][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:27][D][custom:074]: led1_pulse_width: 12
[12:02:27][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:27][D][custom:074]: led1_pulse_width: 12
[12:02:27][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[...]
[12:02:40][D][button:010]: 'Power Button' Pressed.
[12:02:40][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:40][D][custom:074]: led1_pulse_width: 21
[12:02:41][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:41][D][custom:074]: led1_pulse_width: 104
[12:02:41][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:41][D][custom:074]: led1_pulse_width: 100
[12:02:41][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:41][D][custom:074]: led1_pulse_width: 100
[12:02:41][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:41][D][custom:074]: led1_pulse_width: 97
[12:02:42][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:42][D][custom:074]: led1_pulse_width: 100
[12:02:42][D][sensor:094]: 'Fan Speed': Sending state 2.00000 with 0 decimals of accuracy
[12:02:42][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:42][D][custom:074]: led1_pulse_width: 99
[12:02:42][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:42][D][custom:074]: led1_pulse_width: 100
[12:02:42][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:42][D][custom:074]: led1_pulse_width: 98
[12:02:43][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:43][D][custom:074]: led1_pulse_width: 100
[12:02:43][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:43][D][custom:074]: led1_pulse_width: 93
[12:02:43][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:43][D][custom:074]: led1_pulse_width: 99
[12:02:43][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:43][D][custom:074]: led1_pulse_width: 100
[12:02:44][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:44][D][custom:074]: led1_pulse_width: 100
[...]
[12:02:52][D][button:010]: 'Power Button' Pressed.
[12:02:52][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:52][D][custom:074]: led1_pulse_width: 319
[12:02:52][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:52][D][custom:074]: led1_pulse_width: 320
[12:02:52][D][sensor:094]: 'Fan Speed': Sending state 3.00000 with 0 decimals of accuracy
[12:02:52][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:52][D][custom:074]: led1_pulse_width: 314
[12:02:53][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:53][D][custom:074]: led1_pulse_width: 318
[12:02:53][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:53][D][custom:074]: led1_pulse_width: 319
[12:02:53][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:53][D][custom:074]: led1_pulse_width: 319
[12:02:53][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:53][D][custom:074]: led1_pulse_width: 313
[12:02:54][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:54][D][custom:074]: led1_pulse_width: 319
[12:02:54][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:54][D][custom:074]: led1_pulse_width: 315
[12:02:54][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:54][D][custom:074]: led1_pulse_width: 319
[12:02:54][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:54][D][custom:074]: led1_pulse_width: 319
[12:02:55][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:55][D][custom:074]: led1_pulse_width: 319
[12:02:55][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:55][D][custom:074]: led1_pulse_width: 319
[12:02:55][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:55][D][custom:074]: led1_pulse_width: 319
[12:02:55][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:55][D][custom:074]: led1_pulse_width: 319
[12:02:56][C][pulse_width:026]: 'Fan Speed' - Got pulse width 0.000 s
[12:02:56][D][custom:074]: led1_pulse_width: 317
LED fan speed was not reliable for me, especially when turning off. I went to PWM as explained in https://github.com/jonathonlui/esphome-ikea-uppatvind/issues/1#issuecomment-1679518188
Hi.
great work :) Sensor fan speed working good on 0, 1 nad 2 value, but when got 3 then showing 3 and quick switching between 0 and 3. It's possible to fix it?