renssies / homebridge-am43-blinds

A homebridge plugin to control the AM43 based blind motors in HomeKit
MIT License
18 stars 6 forks source link

Implement LightSensor service #27

Open marcoveeneman opened 3 years ago

marcoveeneman commented 3 years ago

Hi, i recently got a AM43 to play with and installed this plugin in homebridge in order to control my blinds using HomeKit. This is all working fine but i noticed in the README that the light sensor is not implemented yet. I'd like to contribute this feature but i'm a bit unsure on some detail i'd like to discuss here.

The AM43 light sensor reports the light level as an integer in the range [0-10] (?). The CurrentAmbientLightLevel characteristic of the LightSensor service expects a value in lux, so we need to do a conversion. I could come up with the following solutions:

  1. Don't convert, and display the light level directly as lux. Pros: Easy, no conversion needed, just use the light level as lux. Cons: Value displayed in HomeKit is not representing the actual light level, this may not play well with automations based on lux values.

  2. Convert light level to percentage. Pros: Easy to understand value. 0 means dark, 100 means bright. Cons: Same as 1.

  3. Convert light level to lux Pros: The value displayed in HomeKit can be used like other light sensors. Cons: real lux value will differ from actual lux due to the low resolution (only 10 values). Need input on how many lux each light level represents.

Input on the direction to take is much appreciated. Any ideas?

renssies commented 3 years ago

That would be great, I was planning to implement the light sensor, however, the solar panel from my first unit was defective, and by the time the second unit arrived I had (and still have) some personal things to deal with.

My original plan was to convert the light level to LUX. Also from my understanding, the light level from the device was an integer of 0-100, however, I was never able to test this out.

However, there is also a 4th option: we can create a custom characteristic. This has some pros and cons as well. Pros: The light level is the direct value of the device, making it accurate. Cons: The light level is not visible in HomeKit and automations have to be created in third-party HomeKit apps (like Eve of Home+)

A combination of the two is also possible, having the original CurrentAmbientLightLevel characteristic and a custom one on the same LightSensor service.

My choice would go to a solution that at least uses the CurrentAmbientLightLevel characteristic, even if it's inaccurate.

And since my unit arrived defective it would be great to have a config option to remove the light sensor service.

renssies commented 3 years ago

As for converting the value to LUX, I was planning to build an Arduino setup with a LUX sensor or to get a cheap LUX meter and combine the measurements from that.

marcoveeneman commented 3 years ago

I didn't think about that 4th option yet, i like the idea. Regarding the light levels. I'm seeing values from 0 when completely dark, to 9 in full sunlight, so 10 levels in total -> [0-9]. We can create a simple lookup table for that if we know at what lux the light level increases by 1.

In another repo (https://github.com/buxtronix/am43/blob/master/src/AM43Client.cpp) i noticed there is a flags field in the get position command indicating if the light sensor is present (bit 4) and i can confirm this works. Maybe we can use this to show/hide the light sensor?

renssies commented 3 years ago

Using the bit of get position to add/remove the light sensor service would be a great way of doing it

gielk commented 3 years ago

+1 for this would love to use the sensor. I now have them constantly connected to the power because they use too much power to be on the battery alone or use the solar panel. This is because of "hap_interaction_timeout": 0

marcoveeneman commented 3 years ago

Finally made some time to commit my initial changes, hopefully they will be useful for someone. The value of the light sensor is still working as option 1 of the initial issue description. Conversion from light level to lux is still missing.