Support for ADC-based ambient light sensor
Opened this issue · 1 comments
Based on my tests (as I mentioned in libretiny-eu/libretiny#219), the mere existence of one of the keys: 'day', 'dusk', 'evenfall', 'evening' and 'night' suggest that there's an ADC-based ambient light sensor. There seem to be no configuration of ADC in UPK, but all of my 3 kind of lamps that have this use the same setup, which can be expressed in esphome yaml like this:
sensor:
- platform: adc
id: ambient
name: Ambient
unit_of_measurement: "V"
state_class: "measurement"
pin: P23
And the keys can be expressed as esphome select like this:
globals:
- id: light_sensitivity
type: int
restore_value: no
initial_value: '0'
select:
- platform: template
id: light_sensitivity_setting
name: "Ambient light trigger"
entity_category: config
restore_value: yes
options:
- "Day"
- "Dusk"
- "Evenfall"
- "Evening"
- "Night"
initial_option: "Evenfall"
optimistic: true
set_action:
- logger.log:
format: "Chosen option: %s"
args: ["x.c_str()"]
on_value:
then:
lambda: !lambda |-
if (i == 1)
{
id(light_sensitivity) = 0.157f;
}
else
if (i == 2)
{
id(light_sensitivity) = 1.800f;
}
else
if (i == 3)
{
id(light_sensitivity) = 2.100f;
}
else
if (i == 4)
{
id(light_sensitivity) = 2.180f;
}
else id(light_sensitivity) = 0.0f;
The above thresholds are taken from UPK itself of course.
My python knowledge is yet too basic to try the pull request, so I'm leaving this to someone else to try :)
Thanks for posting this. My device has similar keys and functionality. The light sensitivity setting code above doesn't seem to do anything on my device, but I was able to set up a text sensor to display the light level as read by my device's light sensor, based on your ADC sensor. I used the day
, dusk
, evenfall
, evening
, and night
keys from the original config as voltage thresholds (apparently in mV), though I'm going to end up adjusting them for my own usage since my device's sensor is almost always in the shade.
text_sensor:
- platform: template
id: text_sensor_ambient
name: Ambient Light Sensor
sensor:
- platform: adc
pin: ADC3
id: adc_ambient
name: Ambient ADC Value
unit_of_measurement: "V"
state_class: "measurement"
# This `on_value` trigger will update the text sensor based on the voltage thresholds read from the original config
# in my device's case, they looked like this:
# "day": 0,
# "dusk": 200,
# "evenfall": 1500,
# "evening": 1800,
# "night": 2150,
on_value:
then:
- lambda: |-
if (x > 2.15) {
id(text_sensor_ambient).publish_state("Night");
}
else if (x > 1.8) {
id(text_sensor_ambient).publish_state("Evening");
}
else if (x > 1.5) {
id(text_sensor_ambient).publish_state("Evenfall");
}
else if (x > 0.2) {
id(text_sensor_ambient).publish_state("Dusk");
}
else {
id(text_sensor_ambient).publish_state("Day");
}
I then set my own "light sensitivity trigger" by adding the following condition to my on_press
that handles turning on the light when the PIR pin goes high. In my case, I only want it to turn on the light if it is sufficiently dark outside (basically "evening
" or darker):
binary_sensor:
- platform: gpio
pin: P6
name: PIR Sensor
id: binary_sensor_pir
device_class: motion
on_press:
if:
condition:
sensor.in_range:
id: adc_ambient
above: 1.8
then:
- light.turn_on: light_flood
Separate from this, my device has a PIR sensitivity setting that I think can be inferred from the configuration. I will create an issue to detail what I've found.