-
-
Notifications
You must be signed in to change notification settings - Fork 422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QuantityType.as(PercentType.class) fixes with dimensionless units #3297
Conversation
Signed-off-by: Sami Salonen <ssalonen@gmail.com>
if (Units.PERCENT.equals(getUnit())) { | ||
return target.cast(new PercentType(toBigDecimal())); | ||
} else { | ||
QuantityType<T> inPercent = toUnit(Units.PERCENT); | ||
if (inPercent != null) { | ||
return inPercent.as(target); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should check if the dimension is "dimensionless" and return null
if that is not the case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, agree it would sense that non-compatible units (example meter) are disregarded. It would be even more breaking change but then again how many are relying such odd logic
How to check dimension specifically? I have been using the unit conversion to support transformed units (%/10) as well.
The more general question is if this should be allowed at all. A value of 120% is perfectly fine for a |
Yeah it still will respect the range 0%-100%, just taking the unit into account. Apart from backwards compatibility, I do not see a reason why we would not convert unit accordingly? In a way I find it similar to sending Fahrenheit to Item with Celsius state description |
You can use My comment was more general regarding |
for what it's worth... The use case that surfaced this issue came up with modbus binding and scaling of numeric values for Dimmer item. Modbus binding offers gain/offset profile to scale unitless numbers into QuantityType. It happens to be that the unit syntax allows very convenient way to scale the data with raw data of modbus: Consider we have unsigned byte (uint8 in binding terminology) representing light brightness, from 0% to 100%. This could be scaled using "1 %/255" (as opposed to mystic gain of "0.00392156... %"). (sure there are other ways as well, eg coming up with separate script profiles for read/write ops) The fact that PercentType does not inherit its properties from QuantityType feels like pre-UoM design to me actually. After all, both dimensionless and PercentType represent dimensionless value, the other one having only its values limited to a range. |
Don't get me wrong. If you add the dimension check, I'll merge this one. But In general I think that this should be re-visted in the light of #3282 and a better UoM Handling in general. For me |
Absolutely, I value your input a great deal. I think it is very important the whatever the end decision, things are used in a correct way, and the intent is clear with all these types.
Agreed. For example, I just found another nasty UX issue (IMO): It is not just possible to send state update of
Yes I guess this is the crux of of difference in our understanding. Or to be more precise, I do not have too much of an opinion on the When working with Items accepting It turns out that when using
When I realized I could utilize...well, percents (number with unit %, i.e. QuantityType)... the ambiguity is lost, and intent is clear even when looking at the rule / code / script after 1 year.
To my knowledge Many modbus devices avoid floating points due to historical reason, and instead scale the numbers such that they are integers (e.g. 226 integer in modbus could represent 22.6 Celsius). In some cases offset is needed to avoid overflowing the small data types that are typical with modbus. There are other ways to scale things, sure, but then one needs to build the "handler->item" and "item->handler" gains/offsets by hand separately, an error prone task. This is the reason I created the profile in first place, to allow specifying the gain/offsets once, and the right thing will happen with commands (item->handler) as well. It would be really nice for the user if all items dealing with numbers (Dimmer, Rollershutter, Number, Number:dimension) would work with QuantityType since then you do not have to know java classes such as PercentType, QuantityType and DecimalType... If we would need to distinguish between the different types, would it not mean that we need to add parameters to these "number profiles" to select the "state class" to use? |
Actually, the way the helper method has been implemented, makes it unstable. The return value is dependant on field ordering of Let me know what you think, whether this change still makes sense? |
No, that doesn't make sense. And if I'm not mistaken, |
That is correct. But do note that I kept the original silly behaviour which disregards non-compatible units and just proceeds with the "value" part (toBigDecimal). I can add a test for that What I tried to communicate above that it seems that there is no reliable way to get Dimension from unit, and in a way that is understandable... in a way information is lost, and hard to make distinction with radian angles and dimensionless bare numbers? |
I would prefer to return |
Signed-off-by: Sami Salonen <ssalonen@gmail.com>
All right, incompatible units result now in As a separate note for OH4: I got curious why there is asymmetry between constructors and the For example, one would think that if String constructor allows one to use string state update in scripts:
[It is of course entirely another thing that whether this particular OnOff->PercentType kind of conversion should be supported in the first place) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks.
…enhab#3297) * QuantityType.as(PercentType.class) fixes with dimensionless units Signed-off-by: Sami Salonen <ssalonen@gmail.com> GitOrigin-RevId: ef34e5b
Resolves #3296
Note: this is not changing behaviour of silly things like(changed in 169bd4c)QuantityType("0.05 m")
converted toPercentType
.Note2: this is changing behaviour in backwards-compatible way with dimensionless
QuantityType
sSigned-off-by: Sami Salonen ssalonen@gmail.com