But other brands might use other pins.
I haven’t found a suitable device list for ESPHome, but I did find a good list for Tasmota, and since they both use the same pins it is a good reference
Yes, I realised I was missing the point of needing an MCU of some sort, was kinda hoping to achieve dimmer effect via basic light component Then I remembered I had some experience with Sonoff T4 coding where you can setup slow LED activation like this one:
# Setting up slow LED activation
output:
- platform: esp8266_pwm
id: blue_led
pin: GPIO13
inverted: True
light:
- platform: monochromatic
name: "Sonoff ${device_name} Blue LED"
internal: true
output: blue_led
id: led
and then it hit me - I WAS using software PWM for that purpose. Now I’m on the right track with coding, going to order some dimmers
The light I want to control is not directly attached to the esp board like most of the examples here seem to be. I do have that on another node and use a little while loop like this:
Figured I could make this a bit more awesome so I added a sensor to the esp config that gets the current brightness of the light from home assistant. Once I knew that I was able to make it so that if I hold the button and the light is below 50% it will increase the brightness, if it’s above 50% it will decrease it.
I have a setup on an ESP node where the button can dim or brighten the attached LEDS and will try to merge that code with yours so the same logic can be used on lights not directly attached to the ESP. I’m a bit short on time right now so it won’t be immediately.
I managed to merge the code as mentioned. This uses a touch switch module on a ESPHOME ESP-01 node to control a remote HASS light.
A click toggles the light while holding the switch brightens or dims it. To reverse operation (e.g. if it is dimming and you need it brighten) then release the hold down and go straight to hold down again.
I hope it is helpful.
esphome:
name: $devicename
platform: ESP8266
board: esp01_1m
substitutions:
devicename: light-remote-switch # name of this node
upper_devicename: "Light and Switch" # English Readable name for the node
short_devicename: "LightSwitch" # Short form English readable name
hass_light: "light.study_ceiling" # HASS entity name of the HASS light to be controlled
address_ip: X.Y.Z.A
address_subnet: Y.Y.Y.Y
address_gateway: X.Y.Z.B
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
domain: !secret wifi_domain
reboot_timeout: 5min
manual_ip:
static_ip: $address_ip
gateway: $address_gateway
subnet: $address_subnet
logger:
api:
ota:
globals:
- id: bool_dim_or_bright #false = dim, true = brighten
type: bool
restore_value: no
initial_value: 'false'
switch:
# This is to restart the ESPHome device remotely
- platform: restart
name: ${upper_devicename} - Restart
sensor:
- platform: wifi_signal
name: ${upper_devicename} - WiFi
update_interval: 60s
- platform: homeassistant #a local sensor to hold the HASS light brightness
id: light_brightness_from_hass
entity_id: $hass_light
attribute: brightness
binary_sensor:
- platform: homeassistant # a local binary sensor to hold the HASS light on / off status
id: controlled_light_on_hass
entity_id: $hass_light
- platform: gpio
pin: # Note: This must be true or the ESP-01 will not boot is using a touch switch module (which must be set to be active low)
number: 0
inverted: TRUE
mode: INPUT_PULLUP
id: touch_switch
filters:
- delayed_on: 50ms
- delayed_off: 50ms
on_click:
then:
- if:
condition:
binary_sensor.is_off: controlled_light_on_hass # if light is off, turn it on
then:
- homeassistant.service:
service: light.turn_on
data:
entity_id: $hass_light
else: # else it is on so turn it off
- homeassistant.service:
service: light.turn_off
data:
entity_id: $hass_light
on_press:
then:
- if:
condition:
lambda: |-
return id(bool_dim_or_bright);
# When above condition evaluates to true - brighter function else dimmer
then:
- delay: 0.5s
- while:
condition:
binary_sensor.is_on: touch_switch
then:
- homeassistant.service:
service: light.turn_on
data:
brightness_step: "5"
# transition: "1"
entity_id: $hass_light
- delay: 0.05s
- lambda: |-
id(bool_dim_or_bright) = (false);
else:
- delay: 0.5s
- while:
condition:
and:
- binary_sensor.is_on: touch_switch
# This is to set the minimum value so that touch sensor only allows pre-set minimum
- sensor.in_range:
id: light_brightness_from_hass
above: 10
then:
- homeassistant.service:
service: light.turn_on
data:
brightness_step: "-5"
# transition: "1"
entity_id: $hass_light
- delay: 0.05s
- lambda: |-
id(bool_dim_or_bright) = (true);
I tested it and it works great for me. I immediately flashed it on a sonoff mini which controls an esp led dimmer in my ceiling lamp. I couldn’t figure out how to do it otherwise, while still using my conventional wall switches. Before I had to adjust the dimming through home assistant and the wall switch was used for powering on/off. However, this was not making the wife happy.
Now, she is happy again. Also because now we can controll all led lighting through the momentary wall switches.
binary_sensor:
- platform: gpio
pin:
number: GPIO5
name: ${device_name} button
id: ${device_name} button
# config for multi click actions
on_multi_click:
# double click
- timing:
- ON for at most 1s
- OFF for at most 1s
- ON for at most 1s
- OFF for at least 0.2s
then:
- if:
condition:
and:
- wifi.connected:
- api.connected:
# double click to toggle between cold and warm light
then:
- if:
condition:
lambda: |-
return id(bool_dim_or_bright);
# When above condition evaluates to true - cold else warm
then:
- delay: 0.5s
- while:
condition:
binary_sensor.is_on: ${device_name} button
then:
- light.turn_on:
id: light_1
brightness: 40%
color_temperature: 2000 K
- lambda: |-
id(bool_dim_or_bright) = (false);
else:
- delay: 0.5s
- while:
condition:
and:
- binary_sensor.is_on: ${device_name} button
then:
- light.turn_on:
id: light_1
brightness: 100%
color_temperature: 6530 K
- lambda: |-
id(bool_dim_or_bright) = (true);
# toggle relay in case either wifi or api are not connected
else:
- switch.toggle: shelly_relay
I’m trying to figure out how to toggle cold or warm light with double click using lambda.
But I’m not sure how to configure the condition so double click would just toggle bool_warm_or_cold true or false.
I have edited the code.
In my version, the last dimming direction is saved. So if you dim upwards and then dim down again, the light is dimmed downwards.
With off/on the light has the last birghtness instead of 1.0
still one bug, if you dim to 0 and switch on the brightness will be 1.0, but in this case the lastDirection has the wrong value, you need to dim 2 times.