-
-
Notifications
You must be signed in to change notification settings - Fork 243
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
[blockly] Add common Math operations support for Quantity #2001
Comments
@rkoshak see my additional min/max block as well (I edited the above comment) |
Returns the nearest 32-bit single precision float representation of a number. It basically converts a double to a float if I'm interpreting it correctly. The examples are interesting:
It really isn't a standard round function and I suspect it's not something anyone would need to do in a rule unless they are parsing some modbus register or the like. Maybe it's not worth supporting.
I can see arguments for both ways. Recently I helped with a rule where someone needed to take a Temperature, add an offset to it, then divide by 10. The end result is still a Temperature. If you strip the units you'd have to add them back. Then again, what unit is appropriate after all those operations? What unit do you get by dividing two temperatures? A ratio? So maybe stripping and adding the units back is appropriate as the whole set of operations were intended to correct a value received. |
I would argue that any operation with QuantityTypes can give a predictable QuantityType. Dividing temperature by temperature would give dimensionless. If you go the path of stripping units, you also need a block to get the unit from an item state to be able to add it back at the end. |
I am not talking about math operations like Multiplication or the like. The block that does that allows the second input to be a number (see https://www.openhab.org/docs/configuration/blockly/rules-blockly-uom.html#quantity-computation) What I am questioning is what it means to find the minimum / maximum of an input being dimensionless with one that has a unit. IMHO that doesn't make sense as you "comparing" apples with pies. When comparing to Quantities (see oh_quantity_compare )this isn't allowed either. |
Fully agree. It should require compatible dimensions (not necessarily units as long as they are compatible). comparing miles/hour and kilometer/hour for instance should be allowed. |
Checking the dimension could be something I may add later and note, that this doesn't work for variables as the latter work require a runtime check which bloats the code (which is against OH blockly design principles). However, I may add that as a later enhancement when I have an idea how to show that warning (currently there is nothing like a good way to provide warnings in the blockly editor except an "alert" that I could provide). |
That should already be supported with the
QuantityType throws an exception if the units are not compatible with a reasonable description of the problem. I'm not sure this needs to be handled at the Blockly level. |
Not sure I understand this statement. How would I do this?
Agree, I think theJavascript error messages would be sufficient. |
That doesn’t look good at all. Temperature with Fahrenheit is tricky as the sequence of operations has in impact due to it not being a strictly linear conversion. But I also don’t see logic in this. This is not related to this issue though and should get its own issue. |
Much more reasonable with Code
Logs
Again with kJ
It still doesn't like But the divide and multiply seems reasonable. But from this I conclude that addition and subtraction without units is simply not supported for many (most? all?) quantity types. If that's the case, does it make sense to allow that in Blockly? I see the following options:
|
The result is interesting. 100 °F = 36.1111111 °C. It seems that number is interpreted as a delta of 36.11111111 K. How that happens? Beats me at the moment. I will probably have to run this through a debugger. |
For me, 1 is the preferred option. |
I think 1 is my preferred option as well. |
Perfect, if I get you right, this is exactly what I have implemented. |
I don't think so. If it's still the same as what you described above:
what you've implemented is 2, not 1. 1 would be to throw an error if one of the operands is a Quantity and the other is not for addition, subtraction, and comparison operations. Blockly should not need to do anything special in these cases though. The underlying library will generate the error. |
I would indeed prefer not to have any magic there. After all, the magic can also lead to unintended consequences. What if WZ_Temperatur is updated with different units? What unit would you take for 3? The system unit? The current unit of the item state? I prefer to be clear. That allows to take the maximum of a temperature in °C and °F for instance. |
ok, I would the remove it from the code during the week and then hopefully the new blocks will be available soon. |
Great. I think this is the right appproach. |
To add something: the fact that we expect both inputs to be the same actually allowed my to support variables better in this case. Unfortunately variable types cannot be detected via blockly, so I HAVE to assume the type of the variable. In the case of min/max having two vars, I do have to assume that these are numbers. However, when one of the inputs is not a variable I can now detect that type and base my code generation of the non-var-input-type which is nice. |
Closes #2001. Adds Quantity support for more math blocks: - math_single had to be reimplemented - math_minmax was added Note that there is a special case on min/max if the inputs are not of equal type an error will be shown to user. In the special case of variables Blockly does its best to detect the right code to be generated for the min/max block: - both are variables -> then numerical input is expected - one of inputs is a variable: then blockly uses the type of the other non-var-block to base the generation on (either number or quantity comparison) - note that no type conversion of the inputs is done Also-by: Florian Hotze <florianh_dev@icloud.com> Signed-off-by: Stefan Höhn <mail@stefanhoehn.com>
The problem
Associated with PR #2000.
Common mathematical operations provided by the standard JavaScript
Math
class cannot be used withQuantity
objects.Your suggestion
It probably doesn't make sense to support all of the possible operations but the following list makes sense to me.
abs
ceil
floor
max
min
pow
(I can go either way on this since raising a Quantity to a power likely changes the unit which obviously wouldn't actually happen here by implementing this outside of the QuantityType library)fround
(I'm not sure I've ever seen this used in rules)trunc
(also I'm not sure I've seen this used in rules and the same result can be achieved usingceil
andfloor
I thinkI can't imagine users needing to do trig or log match in rules with units so all that stuff can be skipped.
The text was updated successfully, but these errors were encountered: