openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

Issue with Quantity to QuantityType conversion after upgrade to 4.1 #313

Closed jlaur closed 6 months ago

jlaur commented 6 months ago

After upgrading my production system to 4.1 I have run into an issue related to QuantityType and JavaScript Scripting.

I don't know if the issue is in openhab-js or in the Energi Data Service thing action. The thing action code was not changed recently, so if the problem lies here, it's probably still related to openhab-js changes, perhaps an existing bug in the thing action being exposed by stricter checks on openhab-js side. Anyway, I hope you'll be able to assist figuring out the culprit.

Expected Behavior

No error, the call should produce a correct result like in 4.0.

Current Behavior

This rule code:

var edsActions = actions.get("energidataservice", "energidataservice:service:energidataservice");
var price = edsActions.calculatePrice(time.Instant.now(), time.Instant.now().plusSeconds(360600), Quantity("150 W"));

Results in:

2023-12-23 16:01:15.535 [ERROR] [enhab.automation.script.file.test.js] - Failed to execute rule Test-JavaScript-8553ef2d-9a4e-484f-ba06-81ada6e26649: TypeError: invokeMember (calculatePrice) on org.openhab.binding.energidataservice.internal.action.EnergiDataServiceActions@11d3737 failed due to: Cannot convert '[object Object]'(language: JavaScript, type: A) to Java type 'org.openhab.core.library.types.QuantityType': Unsupported target type.: TypeError: invokeMember (calculatePrice) on org.openhab.binding.energidataservice.internal.action.EnergiDataServiceActions@11d3737 failed due to: Cannot convert '[object Object]'(language: JavaScript, type: A) to Java type 'org.openhab.core.library.types.QuantityType': Unsupported target type.
    at <program> (test.js:18)
    at _run (@openhab-globals.js:2)
    at execute (@openhab-globals.js:2)
    at execute (@openhab-globals.js:2)
2023-12-23 16:01:15.541 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'Test-JavaScript-8553ef2d-9a4e-484f-ba06-81ada6e26649': Failed to execute action: 1(Error: Failed to execute rule Test-JavaScript-8553ef2d-9a4e-484f-ba06-81ada6e26649: TypeError: invokeMember (calculatePrice) on org.openhab.binding.energidataservice.internal.action.EnergiDataServiceActions@11d3737 failed due to: Cannot convert '[object Object]'(language: JavaScript, type: A) to Java type 'org.openhab.core.library.types.QuantityType': Unsupported target type.: TypeError: invokeMember (calculatePrice) on org.openhab.binding.energidataservice.internal.action.EnergiDataServiceActions@11d3737 failed due to: Cannot convert '[object Object]'(language: JavaScript, type: A) to Java type 'org.openhab.core.library.types.QuantityType': Unsupported target type.
    at <program> (test.js:18)
    at _run (@openhab-globals.js:2)
    at execute (@openhab-globals.js:2)
    at execute (@openhab-globals.js:2))

The thing action code is here:

https://github.com/openhab/openhab-addons/blob/dc187c9eec267cd4c6fa57d883db48b281f8fad8/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActions.java#L138-L151

Third parameter, which I suspect is related to the issue, is defined as:

@ActionInput(name = "power", type = "QuantityType<Power>") QuantityType<Power> power

Possible Solution

Currently none.

Steps to Reproduce

Context

Unfortunately most EDS thing actions are broken, thus automations for optimizing electricity usage.

Your Environment

florian-h05 commented 6 months ago

The issue is in the Addon code, but it is caused by a recent change to openhab-js, that was required to fix an issue. I had to rename a private property, which broke the conversion inside the add-on.

As a work-around, you can use Quantity(…).rawQtyType, but I’ve already created a PR to fix that: https://github.com/openhab/openhab-addons/pull/16106