# OH3.1 Python - rules variable consistence

**URL:** <https://community.openhab.org/t/oh3-1-python-rules-variable-consistence/125775>\
**Category:** Scripts & Rules\
**Tags:** jsr223, jython, openhab3\
**Created:** [August 22, 2021, 7:55pm UTC](https://community.openhab.org/t/oh3-1-python-rules-variable-consistence/125775 "2021-08-22T19:55:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![froido](https://community.openhab.org/user_avatar/community.openhab.org/froido/32/80367_2.png) [@froido](https://community.openhab.org/u/froido)\
**Post date:** [August 22, 2021, 7:55pm UTC](https://community.openhab.org/t/oh3-1-python-rules-variable-consistence/125775/1 "2021-08-22T19:55:33Z")

</div>

- 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](https://github.com/CrazyIvan359/openhab-helper-libraries/)) 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.

```yaml
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
                            /openhab/userdata/cache/org.eclipse.osgi/310/0/bundleFile/Lib
                            __classpath__
                            __pyclasspath__ /
*******************************************************************************

```

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`.

configuration.py

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

msg_list = 123

```

test.py

```python
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.

```yaml
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?

---

<div class="post-metadata">

**Author:** ![hafniumzinc](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@hafniumzinc](https://community.openhab.org/u/hafniumzinc)\
**Post date:** [August 22, 2021, 8:06pm UTC](https://community.openhab.org/t/oh3-1-python-rules-variable-consistence/125775/2 "2021-08-22T20:06:17Z")

</div>

> [@froido](#):
>
> `msg_list = 123`

> [@froido](#):
>
> `variable_test.log.info("users: {}".format(user_list)) # variable stored in configuration.py`

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

---

<div class="post-metadata">

**Author:** ![froido](https://community.openhab.org/user_avatar/community.openhab.org/froido/32/80367_2.png) [@froido](https://community.openhab.org/u/froido)\
**Post date:** [August 23, 2021, 5:42am UTC](https://community.openhab.org/t/oh3-1-python-rules-variable-consistence/125775/3 "2021-08-23T05:42:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hafniumzinc](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@hafniumzinc](https://community.openhab.org/u/hafniumzinc)\
**Post date:** [August 23, 2021, 7:38am UTC](https://community.openhab.org/t/oh3-1-python-rules-variable-consistence/125775/4 "2021-08-23T07:38:40Z")

</div>

> [@froido](#):
>
> `from configuration import user_list`

In my Jython rules the above line lives under

> [@froido](#):
>
> `from core.triggers import when`

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

---

<div class="post-metadata">

**Author:** ![CrazyIvan359](https://community.openhab.org/user_avatar/community.openhab.org/crazyivan359/32/83351_2.png) [@CrazyIvan359](https://community.openhab.org/u/CrazyIvan359)\
**Post date:** [September 15, 2021, 12:50pm UTC](https://community.openhab.org/t/oh3-1-python-rules-variable-consistence/125775/5 "2021-09-15T12:50:57Z")

</div>

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?
