I have got a 4 Channel AC Dimmer with I2C interface at 8266 (different versions). Every part works.
Now I want to link all together and integrate it to Home Assistant.
I want to have 4 monochromatic lights in Home Assistant (because of 4 channels of dimmer), which sends data from Home Assistant to the dimmer via ESPhome via I2C.
It is very simple to set by 3 byte: I2C-Address, Channel-Address, Dimmer value (100 to 0, 100 = dark, 0 = bright).
I do not know, how to configure the i2c output.
#include "esphome.h"
class MyCustomI2c : public Component, public FloatOutput {
public:
void setup() override {
Wire.begin();
}
void write_state(float state) override {
// delay(1000);
int chan = id(channel);
int i2c_state = 100 - int(state * 100);
ESP_LOGD("custom", "The value of chan is: %x", chan);
ESP_LOGD("custom", "The value of state is: %f", state);
ESP_LOGD("custom", "The value of i2c state is: %d", i2c_state);
Wire.beginTransmission(0x23); //address of MCP23017
Wire.write(chan); // port a
Wire.write(i2c_state); // write all pins high
Wire.endTransmission();
}
};
You can use the log or comment it out.
The ac_dimmer.yaml
esphome:
name: ac_dimmer
platform: ESP8266
board: nodemcuv2
includes:
- my_custom_i2c.h
wifi:
ssid: "FRITZ!Box 7490"
password: "my password"
captive_portal:
# Enable logging
logger:
esp8266_store_log_strings_in_flash: false
# Enable Home Assistant API
api:
ota:
i2c:
frequency: 100
sda: D2
scl: D1
scan: True
globals:
- id: channel
type: int
# initial_value works. you can see it in the logs
# initial_value: '0x82'
output:
- platform: custom
type: float
outputs:
id: custom_i2c_output
lambda: |-
auto mycustomi2c = new MyCustomI2c();
App.register_component(mycustomi2c);
return {mycustomi2c};
light:
- platform: monochromatic
name: "Kitchen Light 1"
id: "kl1"
output: custom_i2c_output
on_turn_on:
- logger.log: "Reached 1 on"
- globals.set:
id: channel
value: '0x80'
- logger.log:
format: "The channel value is %x"
args: [ 'id(channel)' ]
on_turn_off:
- logger.log: "Reached 1 off"
- globals.set:
id: channel
value: '0x80'
- logger.log:
format: "The channel value is %x"
args: [ 'id(channel)' ]
# this repeats 3 times for every light.
- platform: monochromatic
name: "Kitchen Light 2"
id: "kl2"
output: custom_i2c_output
on_turn_on:
- logger.log: "Reached 2 on"
- globals.set:
id: channel
value: '0x81'
- logger.log:
format: "The channel value is %x"
args: [ 'id(channel)' ]
on_turn_off:
- logger.log: "Reached 2 off"
- globals.set:
id: channel
value: '0x81'
- logger.log:
format: "The channel value is %x"
args: [ 'id(channel)' ]
# and so on...
It is working! You can remove all that logger stuff. It’s only for me to understand what’s happening. But: It only works for ON and OFF. You can dim. But only the last channel that was switched on or off. light does not have a on_state_changed. The globals.set action will only touched on on_turn_on / on_turn_off .
I am a greenhorn in HA and ESPHome. At the very beginning. If you can make it work, write!
On next step I will try to control via MQTT.