OH3.1 Python - rules variable consistence

  • Platform information:
    • Hardware: PI4
    • OS: Hypriot
    • openHAB version: 3.1 in Docker

Hi, I am using Python in combination with the helper librarys modified by @CrazyIvan359 (link) to create my OH rules. Some of my variables were stored as ‘global’ variables in the configuration.py file on my previous system (OH2.5.12) successfully. With the update to OH3.1 it looks like, this is no longer possible.

2021-08-22 19:52:43.374 [WARN ] [jython.startup                      ] - 
Jython version:             2.7.2.final
Operating system:           Linux
OS Version:                 4.19.118-v7l+
Java vendor:                Azul Systems, Inc.
Java VM name:               OpenJDK Client VM
Java runtime name:          OpenJDK Runtime Environment
Java runtime version:       11.0.11+9-LTS
configuration.py installed: True
sys.path:                   /openhab/conf/automation/lib/python

A short example: The variable msg_list in configuration.py is initialized as 123. In a cron-script (test.py) msg_list from configuration is imported and changed to 101010.


# -*- coding: utf-8 -*-
LOG_PREFIX = "jsr223.jython"

msg_list = 123


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

@rule("Script name")
@when("Time cron 0/10 * * * * ?")
def variable_test(event):

    from configuration import user_list

    variable_test.log.info("msg_list: {}".format(msg_list))  # variable stored in configuration.py
    user_list = 101010
    variable_test.log.info("msg_list: {}".format(msg_list))  # changed variable

At the first run the script should show the initial value of the variable ‘msg_list’. After the first execution of the script the variable should always be 101010. But each time the script is executed it shows the variable msg_list with the value 123 before it is changed again.

2021-08-22 19:53:50.680 [DEBUG] [e.automation.internal.RuleEngineImpl] - The trigger 'Time_cron_0_10_892b4970038211ecb42bd54682d23759_895a96cf038211eca85dd54682d23759' of rule '71faee3c-f843-4bc7-ae67-f469970b667e' is triggered.

2021-08-22 19:53:50.685 [INFO] [jsr223.jython.Test script           ] - msg_list: 123

2021-08-22 19:53:50.688 [INFO] [jsr223.jython.Test script           ] - msg_list: 10101

2021-08-22 19:53:50.690 [DEBUG] [e.automation.internal.RuleEngineImpl] - The rule '71faee3c-f843-4bc7-ae67-f469970b667e' is executed.

2021-08-22 19:54:00.681 [DEBUG] [e.automation.internal.RuleEngineImpl] - The trigger 'Time_cron_0_10_892b4970038211ecb42bd54682d23759_895a96cf038211eca85dd54682d23759' of rule '71faee3c-f843-4bc7-ae67-f469970b667e' is triggered.

2021-08-22 19:54:00.685 [INFO ] [jsr223.jython.Test script           ] - msg_list: 123

2021-08-22 19:54:00.690 [INFO] [jsr223.jython.Test script           ] - msg_list: 10101

2021-08-22 19:54:00.691 [DEBUG] [e.automation.internal.RuleEngineImpl] - The rule '71faee3c-f843-4bc7-ae67-f469970b667e' is executed.

In OH2.5.12 this kind of variable usage was not a problem and in a ‘normal’ Python environment this is possible too. Same problem occurs if any other file import is used, not only with configuration.py. Can someone help my with this issue?

user_list isn’t stored in your snippet of your configuration.py

Oh shit! Thanks for this hint @hafniumzinc !
That was ‘just’ a copy paste error when creating the minimal example. I have corrected it in the post.

In my Jython rules the above line lives under

rather than in the rule itself, and I haven’t had any issues. Both OH2 and OH3.

Hey @froido sorry for the late response, I don’t have much time for the forum lately.

Based on the above posts I’m not sure if you got this working or not. The issue you are having may be caused by the changes to the rule engine in OH3. I believe that each rule now runs in its own context so global variables may become more complicated.

For myself I tend to use items for global values used among several rules, but I don’t have many use cases for this. What are you doing with globals that you need them to span different rules in different files?