Failed to execute script: w/ TypeError: Cannot read property "constructor" from undefined

  • Platform information:
    • Hardware: Synology DS720+
    • OS: DSM 7.2
    • openHAB version: latest 4.0.3
  • Issue of the topic: please be detailed explaining your issue
  • Please post configurations (if applicable):
    • Rules code related to the issue

Hi guys,

unfortunately my rule/script doesn’t work anymore after updating to OH 4.0.3 whenever I set the ‘targetSoC_var’ to 100% or more. I’m sure it’s me (and I could use max. 99%) but I have no clue what I did wrong.
The script starte when a PowerState changes from OFF to ON:

var eS_Powerstate_var, actualSoC_var, targetSoC_var, eS_Gesamtverbrauch_var, log_cycleenergy_var;


eS_Powerstate_var = items.getItem('eScooterPlug_PowerState').state;
items.getItem('eScooterPlug_VerbrauchCycle').postUpdate(Quantity('0 kWh'));
actualSoC_var = Quantity(items.getItem('generic_escooter_actualbatterystatus').state);
targetSoC_var = Quantity(items.getItem('generic_escooter_targetbatterystatus').state);
eS_Gesamtverbrauch_var = Quantity(items.getItem('eScooterPlug_Gesamtverbrauch').state);
items.getItem('log_cycleenergyscooter').postUpdate(Quantity(items.getItem('eScooterPlug_Gesamtverbrauch').state));
if (actualSoC_var >= targetSoC_var) {
  items.getItem('eScooterPlug_PowerState').sendCommand('OFF');
} else {
  while (!((Quantity(eS_Gesamtverbrauch_var).subtract(Quantity(log_cycleenergy_var)).greaterThanOrEqual(Quantity(targetSoC_var).divide(Quantity('100%')).multiply(Quantity('1 kWh')).subtract(Quantity(actualSoC_var).divide(Quantity('100%')).multiply(Quantity('1 kWh'))))) || eS_Powerstate_var == 'OFF')) {
    eS_Gesamtverbrauch_var = Quantity(items.getItem('eScooterPlug_Gesamtverbrauch').state);
    eS_Powerstate_var = items.getItem('eScooterPlug_PowerState').state;
  }
  items.getItem('eScooterPlug_PowerState').sendCommand('OFF');
  items.getItem('generic_escooter_actualbatterystatus').postUpdate(targetSoC_var);
  items.getItem('eScooterPlug_VerbrauchCycle').postUpdate((Quantity(eS_Gesamtverbrauch_var).subtract(Quantity(log_cycleenergy_var))));
  items.getItem('eScooterPlug_VerbrauchTotal').postUpdate((Quantity(eS_Gesamtverbrauch_var).subtract(Quantity(log_cycleenergy_var)).add(Quantity(items.getItem('eScooterPlug_VerbrauchTotal').state))));
}

I deactivated the log_countercycleenergyescooter because this one causes another fault.

  • If logs where generated please post these here using code fences:
2023-09-28 10:46:46.977 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-09-28 10:46:49.077 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-09-28 10:46:50.469 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-09-28 10:46:54.419 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: TypeError: Cannot read property "constructor" from undefined
	at <js>.O(@openhab-globals.js:2) ~[?:?]
	at <js>.c(@openhab-globals.js:2) ~[?:?]
	at <js>.getQuantity(@openhab-globals.js:2) ~[?:?]
	at <js>.:program(<eval>:13) ~[?:?]
	at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[?:?]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262) ~[java.scripting:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:71) ~[?:?]
	at java.util.Optional.ifPresent(Optional.java:178) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:68) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1188) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:997) ~[?:?]
	at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:87) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
	at java.lang.Thread.run(Thread.java:833) ~[?:?]
2023-09-28 10:46:54.422 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'rule_escootercharger' failed: org.graalvm.polyglot.PolyglotException: TypeError: Cannot read property "constructor" from undefined
2023-09-28 10:47:33.939 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined

Unfortunately I wasn’t able

Add lots of logging to narrow it down to a specific block/line of code. From there maybe we have something to go on.

Beyond that, any time you run into cannot <do something> from undefined it means you are trying to reference a variable that does not exist.

1 Like

Thanks!
How can add more logs? That’s all I get when the rule gets triggered.

First I’ll recheck my variables.

1 Like

My script:

var actualSoC_var, targetSoC_var, eS_Powerstate_var, eS_Gesamtverbrauch_var, log_cycleenergy_var;


items.getItem('eScooterPlug_VerbrauchCycle').postUpdate(Quantity('0 kWh'));

actualSoC_var = Quantity(items.getItem('generic_escooter_actualbatterystatus').state);
targetSoC_var = Quantity(items.getItem('generic_escooter_targetbatterystatus').state);
if (actualSoC_var >= targetSoC_var) {
  items.getItem('eScooterPlug_PowerState').sendCommand('OFF');
} else {
  eS_Powerstate_var = items.getItem('eScooterPlug_PowerState').state;
  eS_Gesamtverbrauch_var = Quantity(items.getItem('eScooterPlug_Gesamtverbrauch').state);
  items.getItem('log_cycleenergyscooter').postUpdate(Quantity(items.getItem('eScooterPlug_Gesamtverbrauch').state));
  log_cycleenergy_var = Quantity(items.getItem('log_cycleenergyscooter').state);
  while (!((Quantity(eS_Gesamtverbrauch_var).subtract(Quantity(log_cycleenergy_var)).greaterThanOrEqual(Quantity(targetSoC_var).divide(Quantity('100%')).multiply(Quantity('1 kWh')).subtract(Quantity(actualSoC_var).divide(Quantity('100%')).multiply(Quantity('1 kWh'))))) || eS_Powerstate_var == 'OFF')) {
    eS_Gesamtverbrauch_var = Quantity(items.getItem('eScooterPlug_Gesamtverbrauch').state);
    eS_Powerstate_var = items.getItem('eScooterPlug_PowerState').state;
  }
  items.getItem('eScooterPlug_PowerState').sendCommand('OFF');
  items.getItem('generic_escooter_actualbatterystatus').postUpdate(targetSoC_var);
  items.getItem('eScooterPlug_VerbrauchCycle').postUpdate((Quantity(eS_Gesamtverbrauch_var).subtract(Quantity(log_cycleenergy_var))));
  items.getItem('eScooterPlug_VerbrauchTotal').postUpdate((Quantity(eS_Gesamtverbrauch_var).subtract(Quantity(log_cycleenergy_var)).add(Quantity(items.getItem('eScooterPlug_VerbrauchTotal').state))));
  console.info((['XXX',items.getItem('generic_escooter_actualbatterystatus').state,items.getItem('generic_escooter_targetbatterystatus').state,items.getItem('eScooterPlug_VerbrauchCycle').state,items.getItem('eScooterPlug_Gesamtverbrauch').state,items.getItem('log_cycleenergyscooter').state,items.getItem('eScooterPlug_VerbrauchTotal').state].join('')));
}

The log:

2023-10-01 14:28:40.606 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: Thread was interrupted.
	at <js>.:program(<eval>:15) ~[?:?]
	at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[?:?]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262) ~[java.scripting:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:71) ~[?:?]
	at java.util.Optional.ifPresent(Optional.java:178) ~[?:?]
	at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:68) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1188) ~[?:?]
	at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:997) ~[?:?]
	at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:87) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
	at java.lang.Thread.run(Thread.java:833) ~[?:?]
2023-10-01 14:28:40.609 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'rule_escootercharger' failed: org.graalvm.polyglot.PolyglotException: Thread was interrupted.
2023-10-01 14:29:57.107 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:29:58.619 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:30:00.364 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:30:40.443 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:32:36.553 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:33:16.699 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:33:49.613 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:34:34.338 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:41:18.825 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:41:24.220 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:42:21.859 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:42:33.704 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:42:35.502 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:57:04.142 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:57:06.135 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 14:57:07.732 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 15:12:49.220 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 15:12:50.853 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 15:12:52.560 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined
2023-10-01 15:13:30.568 [INFO ] [ation.script.ui.rule_escootercharger] - XXX90900.408 kWh0.409 kWh0.28 kWh6.008 kWh
2023-10-01 15:21:04.169 [WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn't exist: undefined

The “[WARN ] [e.internal.SseItemStatesEventBuilder] - Attempting to send a state update of an item which doesn’t exist: undefined” is added every single time I start the iOS openHAB app (iOS 17.0.2). So this has nothing to do with my f*cked up rule/script.
Nevertheless for any reason the script is still not working. I rechecked every variable and when I use a ‘targetSoC_var’ below 100% the script keeps running. Even though I’m still not sure whether it is handling my ‘state description’ (%d%%) as ‘%’ or not since the log doesn’t show the unit for both SoC:

[INFO ] [ation.script.ui.rule_escootercharger] - XXX90900.408 kWh0.409 kWh0.28 kWh6.008 kWh

I would expect that the first 2 items are logged as 90%90% instead off 9090.

To be honest I don’t get along with these QTY blocks…
Any suggestions?

Agreed. It means you have a Widget or something open in MainUI that refers to an Item that doesn’t exist, or MainUI is asking for the state of an Item that has not yet loaded.

Well, what type of Item are they? If they are just Number then there are no units involved. If they are Number:Dimensionless did you set the unit metadata on the Item? If not they are being treated as a simple ratio ONE which doesn’t have a unit that shows up when logged (it’s the only unit that behaves this way).

But still, I’m not sure you got my original point. Interleave a log block in between each of the block that make up your script so we can tell exactly which block is throwing the exception. If we cannot narrow the problem down to a specific block/line of code :person_shrugging: .

You have the option to not use units at all. Just set your Items to be Number instead of Number:Something and there will be no more units.

1 Like

I defined them as Number:Dimensionless and set the unit as Metadata (%d %%). After your post I changed them to Number only without removing the Metadata.
Unfortunately I have no idea how to connect the Logging & Output blocks to all the other blocks I’m using already.

Is it still ok/possible to use Metadata to set units for display purposes?

Thank you for your help!

That looks like a state desciption pattern. In OH 4 that only changes how the Item appears in the UI. The unit metadata is required to define the unit you want the Item’s state to carry. They can be different and, when you don’t define the unit metadata often they are different.

Is it still ok/possible to use Metadata to set units for display purposes?

If you’ve not set the unit metadata, your Item is carrying a unit of ONE which is a simple ratio.

You can set the state description pattern to what ever you want. But the unit part will not cause the state of the Item to be converted to that unit. It will just tack the “unit” as a string after the raw state of the Item.

When you use Number:Something and supply a unit in the state description, the state of the Item will actually be converted, where necessary, to that unit for display. For example, if the Item is carrying Wh and the state description says to use kWh, the state of the Item will be divided by 1000 before display. If not, it will just append “kWh” as a string to the Wh value.