Jython: Get Item State

Somehow I can not figure this out by myself and can not find any documentation for this:

I want to access the state of an openHAB item in a Jython script. I tried the following with the bits I found in the forum:

lastState    = Washing_Machine_State.getState()
lastState    = ItemRegistry.getItem("Washing_Machine_State").state 
currentPower = ItemRegistry.getItem("Fritz_Dect_III_Power").getState()

But in all cases I get the error

Failed to execute rule 'd82cf23d-8dc3-4616-96c2-eea00dc96a80': Fail to execute action: 1

When I remove the line the error disappears and the rule is executed (checked with a log message)

I am sure, that this must work somehow, but can’t find the answer


Thanks for your help!

Does

items.Washing_Machine_State

work?

Thanks!

But this gives the same error.

Using the python dir() function I was able to figure it out:

First tipp: Placing the code outside the execute() function while testing gives much more informative error messages (this code is executed when the file is loaded).

One Problem was, that “ItemRegistry” is not defined, I had to use just “ir”

The following examples works for me:

lastState = ir.getItem(“Washing_Machine_State”).getState().intValue()
currentPower = ir.getItem(“Fritz_Dect_III_Power”).getState().floatValue()

events.sendCommand(ir.getItem(“LED_Halle_Flur_Betrieb”), OFF)
events.postUpdate(ir.getItem(“Washing_Machine_State”), 11)

Sometimes the following seems to work too, sometimes it fails. I have not figured out why it i
events.postUpdate(Washing_Machine_State, newState)
events.sendCommand(LED_Halle_Flur_Betrieb, OFF)

Hi,

I have a similar error trying to access the state of an item.

According to the documentation, we can use the following methods:

items["My_Item"]
# or after importing anything within the ``core`` package
items.My_Item
# or
ir.getItem("My_Item").state

This is a simplified version of my code:

from core.rules import rule
from core.triggers import when

@rule("(Py) LivingRoom MotionSensor changed")
@when("Item LivingRoom_Motion received update ON")
def livingroom_motion(event):
    livingroom_motion.log.info(items["LivingRoom_AmbientLight"]) // Works
    livingroom_motion.log.info(items.LivingRoom_AmbientLight)    // Error :frowning: 

However, if try to use the second method I get this error

20:20:53.453 [ERROR] [.(Py) LivingRoom MotionSensor changed] - Traceback (most recent call last):
File “/opt/openhab2/conf/automation/lib/python/core/log.py”, line 51, in wrapper
return fn(*args, **kwargs)
File “”, line 13, in livingroom_motion
AttributeError: ‘org.openhab.core.automation.module.script.internal’ object has no attribute ‘LivingRoom_AmbientLight’

20:20:53.453 [ERROR] [re.automation.internal.RuleEngineImpl] - Failed to execute rule ‘0bdd9b15-2ea8-42c3-9bc8-ddf9e04adf7a’: Fail to execute action: 1
20:20:53.453 [DEBUG] [re.automation.internal.RuleEngineImpl] -
java.lang.RuntimeException: Fail to execute action: 1
at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1199) ~[213:org.openhab.core.automation:2.5.0.201907050304]
at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:995) [213:org.openhab.core.automation:2.5.0.201907050304]
at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:91) [213:org.openhab.core.automation:2.5.0.201907050304]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:?]
at java.lang.Thread.run(Thread.java:748) [?:?]
Caused by: org.python.core.PyException
at org.python.core.Py.AttributeError(Py.java:205) ~[?:?]
at org.python.core.PyObject.noAttributeError(PyObject.java:1013) ~[?:?]
at org.python.core.PyObject.getattr(PyObject.java:1008) ~[?:?]
at core.rules$py.execute$4(/opt/openhab2/conf/automation/lib/python/core/rules.py:52) ~[?:?]
at core.rules$py.call_function(/opt/openhab2/conf/automation/lib/python/core/rules.py) ~[?:?]
at org.python.core.PyTableCode.call(PyTableCode.java:167) ~[?:?]
at org.python.core.PyBaseCode.call(PyBaseCode.java:307) ~[?:?]
at org.python.core.PyBaseCode.call(PyBaseCode.java:198) ~[?:?]
at org.python.core.PyFunction.call(PyFunction.java:482) ~[?:?]
at org.python.core.PyMethod.instancemethod___call__(PyMethod.java:237) ~[?:?]
at org.python.core.PyMethod.call(PyMethod.java:228) ~[?:?]
at org.python.core.PyMethod.call(PyMethod.java:218) ~[?:?]
at org.python.core.PyMethod.call(PyMethod.java:213) ~[?:?]
at org.python.core.PyObject._jcallexc(PyObject.java:3626) ~[?:?]
at org.python.core.PyObject._jcall(PyObject.java:3658) ~[?:?]
at org.python.proxies.core.rules$_FunctionRule$16.execute(Unknown Source) ~[?:?]
at org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandlerDelegate.execute(SimpleRuleActionHandlerDelegate.java:34) ~[?:?]
at org.openhab.core.automation.module.script.rulesupport.internal.delegates.SimpleActionHandlerDelegate.execute(SimpleActionHandlerDelegate.java:60) ~[?:?]
at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1191) ~[?:?]

 7 more

Does anyone know if an extra thing is needing to use this second alternative?

thanks in advance

I think that I didn’t understand well the documentation the first time :disappointed:

After putting the following line everything works as expected :grinning:

import core

EDIT: Does anyone know why this doesn’t work with the from import statements?

For example, I was using two submodules (rules, trigger) from the core and it didn’t work.

However, the documentation and the core/__init__.py say “This patch will be applied when any module in the core package is loaded.”

1 Like

It should
 I’ll look into it.

1 Like