I am using the jython helpers library with the jython-standalone-2.7.2.jar and I trying to use a simplified util.py example from @rlkoshak in Journey to JSR223 Python 1 of 9
from core.jsr223 import scope
from core.actions import NotificationAction
from configuration import admin_email
def send_info(message, logger):
logger.info("[INFO ALERT] {}".format(message))
but when calling send_info in my test rule file specifying that the string has unicode with u"string":
from core.rules import rule
from core.triggers import when
import personal.util
reload(personal.util)
from personal.util import send_info
@rule("JythonInfoTest", description="Jython Info Test", tags=["info"])
@when("Item SwitchJythonInfoTest changed")
def jython_info_test_rule(event):
if items.SwitchJythonInfoTest == ON:
send_info(u"Máquina Lavar Roupa - Começou!", jython_info_test_rule.log)
else:
send_info(u"Máquina Lavar Roupa - Terminou!", jython_info_test_rule.log)
I have an error:
2020-07-31 10:01:11.090 [ERROR] [jsr223.jython.JythonInfoTest ] - Traceback (most recent call last):
File "/etc/openhab2/automation/lib/python/core/log.py", line 51, in wrapper
return fn(*args, **kwargs)
File "<script>", line 25, in jython_info_test_rule
File "/etc/openhab2/automation/lib/python/personal/util.py", line 6, in send_info
out = str(message)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 1: ordinal not in range(128)
2020-07-31 10:01:11.101 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule '458332f8-34da-4007-9ec7-9d1921b7a08e': Fail to execute action: 1
Regarding this, you are absolutely right but I have also notifications in the module like the example and decided to use the whole example. But you are right it complicates more then it helps…
I did not remember to test this , thanks for the input.
After the test the string is still not formatted correctly:
Does you script look the same as in the first post? You need to use unicode strings everywhere you are using unicode.
Passing a logger as an argument is a bad idea for several reasons. A big one being that you do not know the source of the logs. If you need to log in your module, just create a logger for use in the module…
from core.log import logging, LOG_PREFIX
LOG = logging.getLogger("{}.personal.utils".format(LOG_PREFIX))