-
-
Notifications
You must be signed in to change notification settings - Fork 428
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
Parameters injected to JS transformations are not resetted across further invocations of the transformation script #4414
Comments
This issue has been mentioned on openHAB Community. There might be relevant details there: https://community.openhab.org/t/openhab-4-3-milestone-discussion/158139/48 |
As an explanation for the current behavior, the transformation gets loaded and instantiated as an Object. On each call to the transformation, the same Object is reused. That means any variables, constants, and such will be preserved from one call to the next. The same is true for Script actions. One gotcha with transformations is that the same transformation "Object" is reused across all usages of the transformation. So if you use it on two different Things, for example, you can have variables set from one Thing when running the transformation on the second Thing. Note, for JS Scripting, I'm pretty sure there is a lock in place to prevent two Things (for example) from using the transformation at the same time. A Multi-threaded exception would be thrown if that were to happen. I do like the idea that arguments passed into the transformation as arguments be reset at the end of the transformation automatically. Otherwise one cannot support optional arguments. Finally:
are you familiar with the round profile? |
Yes, I am familiar with proper rounding. But in dealing with incoming measurements I prefer reducing the item "flickering" using the concept of significant figures, e.g. in my case default=2 for temperatures, default=5 for athmospheric pressures and 3 for Watts. My above mentioned transformation script significant.js takes care of this and I wanted the additional parameter fig to help me overrule my defaults... |
I think it's probable niche but overall I don't have a problem with it. I see no reason why that wouldn't be accepted in a PR. That doesn't really close this issue though which still needs to be addressed. |
Background
(I'm on Openhab 4.3.0 M2 on a Raspberry Pi 4. This is the first issue I filed for Openhab, pls bear with me for any mistakes. My Openhabian system is manually upgraded to bullseye, although this is not supported... But I thought this issue still might be worth to get investigated since it is easily reproducible...)
Problem: When passing adding additional parameters to toItemScript as with
?fig=2
in the following snippet...this parameter is shared across all invocations of significant.js, i.e. fig is always 2 when significant.js is called, even when it is not explicitly given as in my second item above.
This is rather surprising behavior, and I was wondering whether it's a bug or a feature in M2 (or before?)
PS: Update: It seems that one has to manually set the injected variable to undefined at the end of the transformation script in order to have reset before the next invocation - IMHO this is not an easily expected behavior that at least should be well documented:
fig = undefined; // reset fig to undefined for the next invocation
As suggested in the forum by Florian H. I'm also filing an issue here:
Expected Behavior
I'd expect any parameters that are NOT passed specifically (e.g. "?param1=val1¶m2=val2") always be set to undefined....
Current Behavior
When passing adding additional parameters (e.g. "significant.js?param1=val¶m2=val") to a JS transformation script via toItemScript() - as with
?fig=2
above - these parameters are shared across all invocations of significant.js, i.e. fig is always 2 when significant.js is called, even when it is not explicitly given as in my second item abovePossible Solution
Injected parameters should be implicitly reset to undefined before further invocations of the transformation script
Steps to Reproduce (for Bugs)
Here's is a JS transformation to reproduce:
Use this as sample transformation as in
Number:Energy Refoss_TotalYesterday "Total Yesterday [%d %unit%]" { channel="mqtt:topic:openhab:refossp11b:yesterday" [profile="transform:JS",toItemScript="donothing.js?arg1=ONE"], expire="90m" }
Number:Energy Refoss_TotalToday "Total Today [%d %unit%]" { channel="mqtt:topic:openhab:refossp11b:today" [profile="transform:JS",toItemScript="donothing.js?arg2=TWO"], expire="90m" }
will give output like this, showing the behavior:
Context/Rationale
Background: The significant.js script mentioned initially is my transformation script for rounding incoming values from sensors to a more sensible, reduced number of significant figures/digits depending on the unit of the measurement. [ADDED: This would cause fewer updates for the values actually fed into the receiving item.] I want to use fig= to override my unit specific default, e.g. 2 for temperatures.)
Your Environment
The text was updated successfully, but these errors were encountered: