Bug in jython / JSR223: Trying to add rules, but item Label must be converted to ASCII

I’m experimenting with Jython script support and stumpled upon a bug when trying to register a rule for several items.

The error message I got was:

17:19:08.175 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/homie_monitoring.py': UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 49: ordinal not in range(128) in <script> at line number 26      

The script looked like:
(it’s work in progress and contains some non-working code commented out and maybe some unused imports)

se.importPreset("RuleSimple")
se.importPreset("RuleSupport")

from openhab.triggers import item_triggered
from openhab.actions import Telegram
from openhab.log import logging
from openhab import triggers


rlog = logging.getLogger("RULES.Monitor.Homie")

class monitorHomieDevices(SimpleRule):
    def __init__(self):
        lm_reg = itemRegistry.getItem('homieOnline')
        for i in lm_reg.getAllMembers():
            rlog.debug("Item added: " + str(i))
        triggers = []
        #return "homie_online changed!"


#@item_triggered("message_info")
#def sendInfoMessage():
#    Telegram.sendMessage("Test:" + str(message_info))


automationManager.addRule(monitorHomieDevices())

Idea of the script is to iterate over all items of a group and add a ItemChangedTrigger for each group member.

u’\xfc’ is the ‘ü’ character. So there is definitely no ‘ü’ in the script and also none in the items names.

However, there was a ‘ü’ in one item’s label:

Switch kueche_online   "Küchen-Controller [MAP(ONLINE.map):%s]" (Kueche, homieOnline, rrd_graph)		{mqtt="<[local:homie/kueche/$online:state:MAP(binary.map)]"}

Changing the label to “Kuechen-Controller [MAP(ONLINE.map):%s]” solved the issue.

However, the label is for representation only, so there is something seriously wrong, if someone wants to convert the label to python code?!

I wanted to file a bug for this, but I have absolutely no idea, which component is responsible for this?
Is it openhab-core? eclipse-smarthome? openhab2-addons? Or openhab2-jython? (@steve1 ?).

This isn’t a bug in Jython or JSR223. Note that Jython is based on CPython 2.7 which has both unicode and non-unicode Python string representations (unlike Python 3, which is all unicode).

This following code is causing your issue.

The i variable is a Java Item instance. The Python standard str function only supports the ascii character set. Since this is a Java instance, the function will call the item’s toString method which will return a string with non-ascii characters in this case.

There are multiple ways to fix the problem. You can use the unicode function instead of str to convert the Item instance to a unicode string.

rlog.debug("Item added: " + unicode(i))

Another option is to use the logging format specifiers. For example,

rlog.debug("Item added: %s", i)

Thanks for the hint. I was confused, because the error was shown in the line of the constructor call, not in the line within self. So I thought it was a problem with the class itself, not with an output of it.

I changed everything to unicode() or format strings, now it runs fine.