Skip to content

Instantly share code, notes, and snippets.

@zeroping
Last active April 15, 2023 04:05
Show Gist options
  • Save zeroping/1679b9d32e38f6e982bc7e445548ff7b to your computer and use it in GitHub Desktop.
Save zeroping/1679b9d32e38f6e982bc7e445548ff7b to your computer and use it in GitHub Desktop.
Esphome config for ESP32-based Linkind dimmer switches
# Basic Config
#---
#substitutions:
# # https://esphome.io/guides/configuration-types.html#substitutions
# device_name: esp-br3 # hostname & entity_id
# transition_length: 0s # transition when turning on/off
## Uses a JST24A triac and a second MCU for dimming
esphome:
# https://esphome.io/components/esphome
name: ${device_name}
on_boot:
priority: 400
then:
- lambda: !lambda |-
ESP_LOGI("i2cdimmer", "send dimmer init" );
const uint8_t tosend [] = {0x2A, 0x07, 0x56, 0x01, 0x11, 0xFF, 0x66};
ErrorCode ret = myi2cbus->write(0x50, tosend, 7);
ESP_LOGI("i2cdimmer", "result: %d", (int)ret );
esp32:
board: esp32dev
framework:
type: esp-idf
sdkconfig_options:
CONFIG_FREERTOS_UNICORE: y
wifi:
# should get set by wifi.yaml
#captive_portal:
# doesn't work under esp-idf
#web_server:
#port: 80
# https://esphome.io/components/web_server.html
# doesn't work under esp-idf
logger:
# https://esphome.io/components/logger
api:
#password: !secret esphome_api_password
# https://esphome.io/components/api
reboot_timeout: 0s #disable auto-reboot if homeassistant is not connecting
ota:
#password: !secret esphome_ota_password
# https://esphome.io/components/ota
i2c:
sda: GPIO4
scl: GPIO22
sda_pullup_enabled: true
scl_pullup_enabled: true
frequency: 100kHz
scan: false
id: myi2cbus
#light:
#- platform: status_led
#name: "Switch state"
#pin: GPIO2
light:
- platform: monochromatic
# https://esphome.io/components/light/monochromatic.html
name: ${device_name} dimmer
output: dimi2c
default_transition_length: ${transition_length}
#gamma_correct: 0 # if we want to disable
id: dimmer
on_turn_on:
- output.turn_on:
id: green_led
on_turn_off:
- output.turn_off:
id: green_led
- platform: binary
id: greenlight
name: ${device_name} green led
output: green_led
- platform: status_led
#id: redlight
name: ${device_name} red led
pin: GPIO26
output:
- platform: template
id: dimi2c
type: float
min_power: 0.1
zero_means_zero: true
write_action:
lambda: !lambda |-
uint8_t dim_val = 255 * state;
uint16_t chk = 65403-dim_val;
const uint8_t tosend [] = {0x2A, 0x09, 0x50 ,0x01, dim_val, 0x00, 0x00, (uint8_t)(chk>>8), (uint8_t)(chk)};
ErrorCode ret = myi2cbus->write(0x50, tosend, 9);
ESP_LOGI("i2cdimmer", "result: %d", (int)ret );
- platform: gpio
# https://esphome.io/components/output/gpio.html
pin: GPIO14
inverted: false
id: green_led
binary_sensor:
- platform: gpio
# https://esphome.io/components/binary_sensor/gpio.html
pin:
number: GPIO32
inverted: true
mode: INPUT_PULLUP
name: ${device_name} Top Button
internal: false
on_click:
- min_length: 50ms
max_length: 350ms
then:
- light.turn_on:
id: dimmer
brightness: 70%
on_multi_click:
- timing:
- ON for at least 350ms
then:
- light.turn_on:
id: dimmer
brightness: 100%
- platform: gpio
# https://esphome.io/components/binary_sensor/gpio.html
pin:
number: GPIO33
inverted: true
mode: INPUT_PULLUP
name: ${device_name} Bottom Button
internal: False
on_click:
- min_length: 50ms
max_length: 350ms
then:
- light.turn_off: dimmer
on_multi_click:
- timing:
- ON for at least 400ms
then:
- light.turn_on:
id: dimmer
brightness: 35%
#- output.turn_on: led2
@drkavnger99
Copy link

Anyway you can share the i2cdimmer library? I can't seem to find it anywhere.

@zeroping
Copy link
Author

zeroping commented May 1, 2022

Yes! Absolutely!
The 'right' answer is to get all of this working with https://esphome.io/components/external_components.html#git and to get this example uploaded to https://www.esphome-devices.com/. I'll see if I can get that done. Until then, here's the file as-is:
https://gist.github.com/zeroping/6801d8bd16ae6e0ca7e9fe1d897181b7

@zeroping
Copy link
Author

zeroping commented May 2, 2022

Hah. So, funny story. When I initially did this, I was looking at doing a local custom component, but then ended up not doing that. All of the I2C writes are actually handled in the lambdas in this file. The i2cdimmer.h include is just a left-over, and you don't need it at all.

I've updated the gist to reflect that.

@drkavnger99
Copy link

I excluded it when I built the firmware and it's working... sort of. Maybe I'm missing something but it's not stepping through brightness settings. I'm going to guess it's not set for granular change like from stock firmware or similar to Tasmota?

@zeroping
Copy link
Author

zeroping commented May 2, 2022

Heh, oddly enough, I haven't run the stock firmware on one of these switches. I think I know what you mean about stepping through brightness settings though.

No, this ESPHome config just implements a total of 4 brightness levels, done with a long or short press of the two buttons (0%, 35%, 70%, 100%). Example: a long press on the "down" button jumps immediately to 35%.

What you want is very probably possible with ESPHome though. Just use the output sections from here to drive the 'float' output.

@drkavnger99
Copy link

Ok so I'm not an idiot LOL. I had wondered about that. Let me dig in and see what I can learn. 20 years in IT 5 years with HA and I still struggle to put my head around this stuff. Thanks for the info and thank you for this config.

@zeroping
Copy link
Author

zeroping commented May 2, 2022

No worries. If you can't figure it out, I might be able to look into it. I know I have seen people do similar stuff with ESPHome, but not exactly this. Not many devices with separate up/down buttons.

This may help (from a 3-button switch): https://community.home-assistant.io/t/martin-jerry-tessan-dimmer-automation-help-solved/107324/6

That said, it is not great having to write dimmer-controller logic code yourself. Would be great if that were a pre-written module you could just connect to the buttons and the output.

@jschwalbe
Copy link

@zeroping Having some troubles flashing this OTA. Any suggestions?

@zeroping
Copy link
Author

Well, dang. I ran into some trouble myself. I currently have one of mine pulled and plugged into a serial cable, trying to figure out what happened. I compiled a slightly-changed version (to enable esp32_ble_tracker and bluetooth_proxy), uploaded via OTA, and it never got back on wifi.

I'm working on this on my end, and will update once I get it working.

@jschwalbe did you also try enabling bluetooth? Or were you using the .yaml as it is above?

@DeadEnded
Copy link

DeadEnded commented Oct 12, 2022

@zeroping & @jschwalbe
I was using this config and wanted to add BLE as well - flashed over serial and everything looked good.
When I went to OTA I got the error Error receiving acknowledge binary size: timed out.
I did some digging, and found out that it's timing out during the upload.
SO - to fix it, comment out BLE and add the SDKConfig value CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10".
Push this OTA to increase the timeout, then after that is successful, uncomment the BLE stuff and it will OTA without error.

I just did this a few minutes ago, and so far everything is working and looks good (I did reassemble the device and put it on mains so power wasn't an issue).

Cheers!
DeadEnd

@balvant813
Copy link

New to ESPHome (hace used Tasmota on this switch)..... How add/activate BLE sensors?

@zeroping
Copy link
Author

@balvant813 when I tried a few months ago, this version of the ESP32 didn't support any of the new BLE code. No idea if it can work now, but I haven't tried it since.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment