Correct illuminance formula and treat special cases#11868
Correct illuminance formula and treat special cases#11868andrei-lazarov wants to merge 3 commits into
Conversation
|
Thanks, I already reverted 1 |
d836902 to
3e9bc38
Compare
3e9bc38 to
3235d63
Compare
|
@MikaFromTheRoof I still don't fully understand this, but I added 0 and 0xffff like in ZHA (as suggested in the linked issue) |
| if (result === 0) return 0; | ||
|
|
||
| if (result === 65535) return undefined; |
There was a problem hiding this comment.
Should use 0x0000 and 0xffff (it makes it easier to identify when searching code).
Should also move the luxScale const outside the illuminance fn for good measure.
Though can't return undefined here; will bite the assertNumber in numeric.
ZHC is tricky to use with anything undefined since it doesn't use TS strict (lets stuff pass through that it shouldn't, but it's a massive refactor to enforce strict).
|
This cluster is a little strange, usually, the attr has a range that is outside the special cases (which we handle in ZH), but for some reason, this one has full range + special cases. And it was updated in latest version of the spec, so, for some reason, this does not appear to be a mistake (or it's just a backwards compat issue now...)... The ZH layer maps non-values to |
Would have also been my suggestion to look how ZHA does it. But I am not deep enough into the ZHC to be able to assess, if it is working for Z2M this way. Thanks, @Nerivec for looking into that! I am more on the Zigbee device building side of that whole process and only did write some external converters for my DIY devices. The way I understand the approach of ZHA is, that you will only get 0 lx, if the raw illuminance reported by the device is exactly zero. As soon as it is above that, the illuminance will be interpreted as 1 lx, even if it mathematically should still be rounded down to 0 lx, right? I am using simple photoresistors, that normally never really read the value 0, even if it is in pitch darkness. I could imagine, that it might be the same for most of the off-the-shelf devices. So even with that fix, I think most of the devices might still never reach the raw illuminance value of 0. But at least it would be possible now to report 0 lx without writing a custom external converter. |
|
ZHA has a completely different architecture, and they only have to work with HA. The approach with that attribute isn't that of ZHA, it's that of the Zigbee specification. The zero return if zero in this PR is correct. |
|
Reading the spec |
db7a69d to
224f362
Compare
|
Can't return undefined in ZHC though, it will fail (see #11868 (comment)). |
|
So should we return something like |
|
I think undefined (null json) is the way to go, but need to fix the whole chain to support it properly. if (Number.isNaN(result) {
return undefined;
}If you search for |

reverted Lx values calculated incorrectly in Z2M #11866
fixes: Illuminance Measurement Attributes (Value 0 and 0xffff) zigbee2mqtt#31431