Hello,
I’m trying to control a Kassil A160 light for reef tanks with ESPHome. This light can be controlled by two 0-10v analogue channels: one to control the intensity and one to control the color.
I have created the circuit to covert 2x ESP8266 PWM outputs to 0-10v analogue signals. And I’m now working on setup ESPHome to control these two PWM outputs.
I reviewed the different light components available in ESPHome and seems that none of them are fit to control a light that requires two channels with one for brightness and one for the color.
I have so created a custom light component (kessil.h) as follow:
#pragma once
#include "esphome.h"
class KessilLight : public Component, public LightOutput {
public:
KessilLight(FloatOutput *brightness_output, FloatOutput *color_output)
{
brightness_output_ = brightness_output;
color_output_ = color_output;
}
LightTraits get_traits() override {
auto traits = LightTraits();
traits.set_supports_brightness(true);
traits.set_supports_color_temperature(false);
traits.set_supports_rgb(false);
traits.set_supports_rgb_white_value(true);
return traits;
}
void write_state(LightState *state) override {
float brightness, red, green, blue, white;
// use any of the provided current_values methods
state->current_values_as_rgbw(&red, &green, &blue, &white, this->color_interlock_);
state->current_values_as_brightness (&brightness);
white = white / brightness;
if (brightness == 0)
{
this->brightness_output_->set_level(0);
this->color_output_->set_level(0);
}
else
{
this->brightness_output_->set_level(brightness);
this->color_output_->set_level(white);
}
}
protected:
FloatOutput *brightness_output_;
FloatOutput *color_output_;
bool color_interlock_;
};
And in my ESPHome .yaml I have added:
esphome:
includes:
- kessil.h
light:
- platform: custom
id: reef_tank_light
lambda: |-
auto kessil_light = new KessilLight(id(reef_tank_light_brightness_pwm), id(reef_tank_light_color_pwm));
App.register_component(kessil_light);
return {kessil_light};
lights:
- name: "Reef Tank - Light"
output:
- platform: pca9685
id: reef_tank_light_brightness_pwm
channel: 0
- platform: pca9685
id: reef_tank_light_color_pwm
channel: 1
This works well to control the light and from HA I can adjust the light intensity (using the brightness slider) and the light color (using the the white slider).
Now I would like to create a sensor that returns the current brightness and white (color) value for this light. The reason for this is that I use very long transitions to reproduce dawn and dusk, and I would like to see brightness and color throughout the transition. When using a standard monochromatic light I would add the following to the ESPHome .yaml file:
- platform: template
name: "Reef Tank - Light Brightness"
id: reef_tank_light_brightness
update_interval: 20s
accuracy_decimals: 0
unit_of_measurement: "%"
icon: "mdi:weather-sunny"
lambda: |-
return (int(id(reef_tank_light).current_values.get_brightness() * 100));
When using the same approach with the custom light I get the following error when trying to compile:
src/main.cpp: In lambda function:
src/main.cpp:720:35: error: 'const class esphome::custom::CustomLightOutputConstructor' has no member named 'current_values'
return (int(reef_tank_light.current_values.get_brightness() * 100));
Any suggestions on how to implement the equivalent of .current_values.get_brightness() and .current_values.get_white() for a custom light?
I have tried several approaches (trying to overwrite get_brightness(), create a custom sensor etc.) but I’m getting nowhere… Any suggestion or hint would be much appreciated.