-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotary encoder light brightness - fine control.yaml
executable file
·83 lines (76 loc) · 2.62 KB
/
rotary encoder light brightness - fine control.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
#******************************************************************************
# ESPHome based "dimmer" - with fine control at low brightness
# Use a rotary encoder connected to ESP32 as a dimmer for a light in Home Assistant.
#
# Function:
# 1. Turn the encoder, sets HA light brightness in steps of 2% from 0-20%, then 10% steps from 20-100%
#
# Code by Patrick Felstead
#******************************************************************************
esphome:
name: rotary_encoder_set_brightness_fine
platform: ESP32
board: esp32dev
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
manual_ip:
static_ip: 192.168.0.25
gateway: 192.168.0.1
subnet: 255.255.255.0
# Enable logging
logger:
#level: INFO
level: DEBUG
api:
ota:
globals:
- id: encoder_light_brightness
type: int
initial_value: '0'
- id: multiplier
type: int
initial_value: '2'
sensor:
#******************************************************************************
# Rotary encoder
# ESPHome docs: https://esphome.io/components/sensor/rotary_encoder.html
#******************************************************************************
- platform: rotary_encoder
name: "Rotary Encoder"
id: knob
pin_a: GPIO2
pin_b: GPIO4
resolution: 2
min_value: 0
max_value: 10
filters:
- debounce: 50ms # debounce the encoder wafers
- throttle: 100ms # limit updates if the user twists the encoder too fast :D
on_value:
then:
- lambda: |-
ESP_LOGW("rotary", "rotary enc=%2.1f", x);
// Use rotary encoder to set the brightness % of a Home Assistant light bulb
// The "multiplier" (global variable) is used to set the % brightness in steps of 2% from 0-20%, and in 10% steps from 20-100%
id(encoder_light_brightness) = (int) x;
if (id(multiplier) == 2) {
if ((int) x >= 10) {
id(knob).set_value(2);
id(encoder_light_brightness) = 2;
id(multiplier) = 10;
}
}
else if (id(multiplier) == 10) {
if ((int) x <= 2) {
id(knob).set_value(10);
id(encoder_light_brightness) = 10;
id(multiplier) = 2;
}
}
ESP_LOGW("Rotary Encoder Sensor", "HA light brightness = %d%%", id(encoder_light_brightness) * id(multiplier));
- homeassistant.service:
service: light.turn_on
data:
entity_id: light.lounge_table_lamp
brightness_pct: !lambda 'return id(encoder_light_brightness) * id(multiplier);'