esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
415 stars 26 forks source link

Support for VEML7700 ambient light sensor #2493

Open joshuaboniface opened 10 months ago

joshuaboniface commented 10 months ago

Describe the problem you have/What new integration you would like I would like a proper integration for the VEML7700 light sensor. At least 4 people, including myself (https://community.home-assistant.io/t/veml7700-with-esphome/469545/) have requested this.

Please describe your use case for this integration and alternatives you've tried:

218 provides two "workarounds", neither of which are compatible.

The first is to use the BH1750 component, which simply does not work. Nor does the TSL2591 component. Both report 0 lux output with the VEML7700.

The second is to use a custom component which directly leverages the Adafruit Arduino libraries. This is both deprecated as per the notice on the custom components article, and incompatible with non-Arduino frameworks, namely ESP-IDF, which can break other components. This is also the "workaround" referenced in the above forum post.

Additional context

218 is over 4 years old, and its "workarounds" are no longer valid.

1915 was erroneously closed as a "duplicate" of the above despite the clearly changing situation re: custom components, and since it has been aggressively locked, there is no opportunity to provide this feedback there. Thus, I am opening a new feature request to ensure this request is properly tracked.

lboue commented 9 months ago

I'd also like to be able to use my Adafruit Right Angle VEML7700 Lux Sensor - I2C Light Sensor - STEMMA QT / Qwiic module.

Kilill commented 9 months ago

I second this as well.

There exists a ESP-IDF driver for this at https://github.com/kgrozdanovski/veml7700-esp-idf

I have not gotten around to test it yet so no idea how well it works, but looking at it (and with the caveat of having no clue about ESPHome modules) it looks like it should not be that difficult to wrap it up in some ESPHOME clothes.

B48D81EFCC commented 9 months ago

Would love to use the VEML7700 with esphome.

latonita commented 9 months ago

I can take a look into this when my sensor arrives in a week or so

latonita commented 9 months ago

@B48D81EFCC @Kilill @lboue @joshuaboniface please try draft version I've just made https://github.com/latonita/esphome-veml7700

gain = one from [X1, X2, X1/4, X1/8] integration time = one of [25, 50, 100, 200, 400, 800] glass_attenuation_factor not yet working

plans to do AUTO gain/time mode, but not sure yet

esphome:
  name: veml7700-test

esp32:
  board: esp32dev
  framework:
    type: arduino

# esp8266:
#   board: nodemcuv2

logger:
  level: DEBUG

external_components:
  - source: github://latonita/esphome-veml7700
    components: [ veml7700 ]

i2c:
  sda: GPIO25
  scl: GPIO32
  scan: true
  frequency: 5kHz

sensor:
  - platform: veml7700
    address: 0x10
    update_interval: 30s
    gain: X1
    integration_time: 100ms
    glass_attenuation_factor: 1.0
    ambient_light:
      name: "Ambient light"
    white:
      name: "White"
lboue commented 9 months ago

please try draft version I've just made https://github.com/latonita/esphome-veml7700

@latonita Thanks for your work, it's great! I've just done a test:

image

Logs

[09:52:18][D][veml7700:140]: Updating
[09:52:18][D][veml7700:035]: Reading sensor data
[09:52:18][D][veml7700:049]: Resolution lx/counts = 0.0576
[09:52:18][D][veml7700:054]: ALS counts = 50979, lux = 2936.4
[09:52:18][D][veml7700:055]: WHITE counts = 53040, lux = 3055.1 
[09:52:18][D][sensor:094]: 'Ambient light': Sending state 2936.39038 lx with 1 decimals of accuracy
[09:52:18][D][sensor:094]: 'White': Sending state 3055.10400 lx with 1 decimals of accuracy
B48D81EFCC commented 8 months ago

Great, thanks a lot. I am going to use/test the sensor on the weekend.

latonita commented 8 months ago

please use PR link to test

external_components:
  - source: github://pr#6067
    components: [veml7700]

and check documentation here https://deploy-preview-3525--esphome.netlify.app/

B48D81EFCC commented 8 months ago

Hi @latonita,

I just gave it a quick try, generally it is working fine. Thanks a lot. I discovered two things:

My first guess was, that the reason is the default integration time of 100ms. I tried to set the integration time to 25m by setting auto_mode: false and integration_time: 25ms but I still got the very same warning.

latonita commented 8 months ago

@B48D81EFCC thanks for testing. I will fix documentation! As for timing, it's definitely not possible to do measurements in a given time in auto mode, I will check the fixed settings if it can be improved. Thanks for your findings.

Kilill commented 8 months ago

Tested with a ESP32 C3 Super mini board (these are now my new favorites when it comes to ESP-Home), works like charm right out of the box! Thanks a lot!

Carpetner commented 8 months ago

Hi @latonita.

Thanks for your great effort, the code from your PR does a far better job under intense light conditions, as my custom sensor (using some arduino lib). The correction math seems to be more accurate.

I get a strange warning when compiling; and even more strangely, same warning appears sometimes between the logs:

In file included from src/esphome.h:111, from src/main.cpp:3: src/esphome/components/veml7700/veml7700.h:65:10: note: offset of packed bit-field 'esphome::veml7700::ConfigurationRegister::<unnamed struct>::ALS_IT' has changed in GCC 4.4 struct { ^

Auto-mode does not work for me, it is constantly re-adjusting (again under intense light conditions). Maybe the the code could wait some time at the lowest gain and int-time for things to settle? My response times are also quite long, between 70 and 120 ms. I have another VEML in the mail and will do some more testing...

latonita commented 8 months ago

@Carpetner

I get a strange warning when compiling; and even more strangely, same warning appears sometimes between the logs:

thats okay, its a "note", we will try to suppress this, but its just an information

Auto-mode does not work for me, it is constantly re-adjusting (again under intense light conditions). Maybe the the code could wait some time at the lowest gain and int-time for things to settle? My response times are also quite long, between 70 and 120 ms. I have another VEML in the mail and will do some more testing...

I would be happy to see full logs, to understand whats happening and how it can be improved. I hope there is no some kind of indefinite loop which is definitely a bug! And for high bright conditions it should work really fast if you leave default gain/time unchanged. What happening is 1) to do a change we need to turn off device first 2) wait for int.time period 3) turn it on 4) to make sure we get stable and good samples we need to wait at least 2, better 3 int.time periods 5) we get good stable reading 6) too dim/bright -> adjust and go to 1) In case we get data too quick - it might be more "noisy" and not reliable.

However, we definitely not in a hurry with this measurements, I don't believe we need anything faster than 60s update ;))

Thanks in advance for full logs.

Carpetner commented 8 months ago

Will provide the logs tomorrow, stupid just smoked his only testing ESP. Might be a loop problem, it was spamming the logs at a nonhuman rate.

I fully agree on the 60s update, however the component blocks the whole ESP for that ~100ms. Are you using delays? Maybe the code could release execution while waiting for int.time period(s), then check again in the next scan cycle.

I plan to use 12 of these for my next project. Wonder if @Kilill experiences the same response times?

latonita commented 8 months ago

Are you using version from PR? github://pr#6067

Carpetner commented 8 months ago

Yes, I'm on PR. Don't know if there were any changes the last few days, but auto-mode works now. Also tested on a "production" ESP with old sensor, where I had the problem originally.

The sensor is now using the 25-1/4 setting all the way down to ~130 lux, only then the gain starts increasing. The measurements seem pretty reasonable, but I will try to verify them with a quality lux-meter.

Also the response time is now steady at 65ms, hope this will not be a merge stopper. Great job!

Carpetner commented 8 months ago

After some further testing, I think my problems were caused by an unclean build environment. It's not the first, and probably not the last time...

latonita commented 8 months ago

@Carpetner i would appretiate if you can upload some logs to find out where is the delay. One of the reason I found on my setup - publishing state is taking long some time, so i had to split publishing in few batches. maybe i will need to split more. thanks

Carpetner commented 8 months ago

This are probably not the logs you were hoping for, but here they are (I'm somehow limited to HA at the moment):

[01:48:07][D][veml7700:117]: Updating
[01:48:07][D][veml7700:119]: Initiating new data collection
[01:48:07][D][veml7700:254]: Reconfigure time and gain (25 ms, 1/8x) Shutting down
[01:48:07][D][veml7700:254]: Reconfigure time and gain (25 ms, 1/8x) Turning back on
[01:48:07][D][veml7700:293]: Data from sensors: ALS = 8717, WHITE = 10661, Gain = 1/8x, Time = 25 ms
[01:48:07][D][veml7700:351]: Lux resolution for (25, 1/8x) = 1.8432 
[01:48:07][D][veml7700:359]: Automatic mode - ALS = 16067.2 lx, WHITE = 19650.4 lx, FAKE_IR = 3583.2 lx
[01:48:07][D][veml7700:388]: Lux compensation - ALS = 38258.8 lx, WHITE = 69556.3 lx, FAKE_IR = 31297.4 lx
[01:48:07][D][veml7700:396]: Glass attenuation - ALS = 38258.8 lx, WHITE = 69556.3 lx, FAKE_IR = 31297.4 lx
[01:48:07][D][sensor:094]: 'Ambient light': Sending state 38258.81641 lx with 1 decimals of accuracy
[01:48:07][D][sensor:094]: 'Full spectrum': Sending state 69556.25781 lx with 1 decimals of accuracy
[01:48:07][D][sensor:094]: 'Near infrared': Sending state 31297.44141 lx with 1 decimals of accuracy
[01:48:07][W][component:214]: Component veml7700.sensor took a long time for an operation (___0.06 s___).
[01:48:07][W][component:215]: Components should block for at most 20-30ms.
latonita commented 8 months ago

@Carpetner looks like it takes a while to send data, i will split data publishing in even smaller pieces

Carpetner commented 8 months ago

Beautiful, the long wait is gone

B48D81EFCC commented 7 months ago

HI @latonita,

it looks like your PR passed all checks and will be merged into the esphome codebase soon, am I correct?

latonita commented 7 months ago

@B48D81EFCC Yes, its correct. Its in the long queue to be reviewed and merged in.

rici44 commented 4 months ago

During building I got errror In file included from src/esphome/components/veml7700/veml7700.cpp:1: src/esphome/components/veml7700/veml7700.h:70:10: note: offset of packed bit-field 'esphome::veml7700::ConfigurationRegister::::ALS_IT' has changed in GCC 4.4 70 | struct { | ^

And after boot sensor was not found [23:15:29][I][i2c.arduino:096]: Results from i2c bus scan: [23:15:29][I][i2c.arduino:098]: Found no i2c devices! [23:15:29][E][veml7700:112]: Communication with I2C VEML7700/6030 failed! [23:15:29][E][component:082]: Component veml7700.sensor is marked FAILED