Strange behaviour logging OH3 JSR223

  • Platform information:
    • Hardware: RPI 3B
    • OS: Stretch
    • Java Runtime Environment: 11
    • openHAB version: 3.01

Hi all,

I have a really weird behavior when logging with JSR223 Python. I have written a function that I pass a list of items and it returns the states as a float. At the same time the function should log the value. But the log never appears when I do it this way. The return values are correct but i don’t get the log.

log = LogAction.logInfo

@rule("Automate blinds depending on sun with Jython", description="Automate blinds depending on the sun angle", tags=["Automate", "Sun"])
@when("Item Test_Output_Item changed")
def move_blinds(event):
    itemList = ["Azimuth", "Elevation"]
    Azimuth, Elevation= getMultipleItemStates(itemList)


def getMultipleItemStates(itemList):
    stateArray = [None] * len(itemList)
    for i, itemInList in enumerate(itemList):
        stateArray[i] = float(str(ir.getItem(itemInList).state))
        log("sunRule.rules", itemInList + ": " + str(stateArray[i]))
    return tuple(stateArray)

If I remove the get ir.getItem… part the log appears.

def getMultipleItemStates(itemList):
    stateArray = [None] * len(itemList)
    for i, itemInList in enumerate(itemList):
        log("sunRule.rules", itemInList + ": " + str(stateArray[i]))
    return tuple(stateArray)

I also tryed to pass two seperate arrays, one for the Items and one for printing.

def move_blinds(event):
    itemList = ["Azimuth", "Elevation"]
    printList = ["Azimuth", "Elevation_"]
    Azimuth, Hoehenwinkel = getMultipleItemStates(itemList, printList)


def getMultipleItemStates(itemList, printList):
    stateArray = [None] * len(itemList)
    for i, itemInList in enumerate(itemList):
        stateArray[i] = float(str(ir.getItem(itemInList).state))
        log("sunRule.rules", printList[i]+ ": " + str(stateArray[i]))
    return tuple(stateArray)

The crazy thing here is, that there is NO log for “Azimuth” but for “Elevation_” there is one. But if I remove the “_” the log disappears.

Some ideas? I spent hours of trying different stuff but nothing worked.

Thanks

I have encountered cases in Python where an error gets suppressed and the code just exits. There is a decorator in the Helper Libraries that will catch some of those (I can’t remember exactly what it’s called but it should be easy to find).

I find it works best that when you are working with Java Objects (any time you interact with openHAB itself you are using Java Objects) it’s best to use the Java way as opposed to the Python way. So instead of str() use .toString(). Assuming ItemInList is a Number Item, instead of using float() use .floatValue().

What is probably going on here is that your Items are not plain old numbers but have units. 90.45 ° for Azimuth, which if you are using the Astro binding here it is a Number:Angle so that is likely the case, Python’s float parser is going to fail to parse it because ° isn’t part of a valid float.

It’s also possible that the Item’s state is NULL or UNDEF which also cannot be parsed to a float.

Thank you for your reply.

I changed the string and float handling like you suggested, thanks for that.
But it didn’t change the log behavior.

The Number was Number:Angle bevor but I changed this because of an error by converting into float.

A workaround for the problem was to rename the item to a name starting with a lowercase letter?!
Didn’t change something on the rule, only the item name. Weird.

Did you remember the decorator for the suppressed errors? Is there a way to disable this?

That probably won’t work because the binding requires it to be a Number:Angle. But it might be smart enough to handle it.

In the long run you’ll want to learn how to deal with units of measurement because most of the bindings are moving to them.

I haven’t used Python for a little over a year now so I don’t remember the decorator but I believe it’s covered in the Helper Library docs. If not you should be able to find it in the code.

I think i found the decorator, did you mean “log_traceback”?

ok now it’s getting a little embarrassing for me… there is no crazy behavior of the logger. It’s all my fault, I had created a filter for some items in log4j2.xml ages ago, including Azimuth and Elevation. So it is clear why the log did not show up.
Sorry for the unnecessary effort, but I learned something anyway.