Trouble setting a rule-global variable (solved)

Tags: #<Tag:0x00007f616eedd830>

i’m trying to track “hot” days and eventually send a notification (or something) when its cool enough outside to open the windows but i’m having trouble storing the prior temperature between temperature changes. i wanted to avoid creating an “item” for it, but is it the only way? here’s my rules so far, the item in question is the “var max = null” seems to always be null at the start of the changed event.

val HOT_DAY_MIN = 82|"°F"
val HOT_DAY_MAX = 78|"°F"

var hotToday = false
var coolNotif = false
var max = null

rule "track hot days"
	Item zwave_device_831e87fd_node10_sensor_temperature changed
	var temp = zwave_device_831e87fd_node10_sensor_temperature.state
	logInfo("weather", "current temperature is: " + temp);
	logInfo("weather", "start: " + temp + ", " + max + ", " + hotToday + ", " + coolNotif)
	logInfo("weather", (temp > HOT_DAY_MIN));
	logInfo("weather", (hotToday === false));
	if (temp > HOT_DAY_MIN && hotToday === false) {
		hotToday = true
		logInfo("weather", "its hot today! (" + temp + " outside)")
	if (max === null || temp > max) {
		logInfo("weather", "setting new max for today " + temp + ", old value " + max + ", hot " + hotToday)
		max = temp
	if (temp < HOT_DAY_MAX && hotToday === true && coolNotif === false) {
		coolNotif = true
		logInfo("weather", "its hot today but its cooling now! (" + temp + " outside, max today " + max + ")")

	logInfo("weather", "end: " + temp + ", " + max + ", " + hotToday + ", " + coolNotif)

rule "reset hot days"
  Time cron "0 0 0 ? * *"
  hotToday = false
  coolNotif = false
  max = null

actually it seems to work now. i had copy/pasted the syntax for temperature constants from various places (old format vs new) and i switched it from F to C in one of those copy pastes so it was trying to get to 82 celsius

In fact there’s nothing wrong with creating an item for that purpose. Will allow to use that in other rules files, to be persisted, graphed etc.

1 Like


I’m interested in global variables too. My Question:
I want to us a variable over different rule files. So I have to guarantee now, the variables are set first.

Would it work, if I create a 0000_variables.rules File with only the variables in, so it is readed first when openhab2 starts?

No. It would need to be an items file.
There are no global variables (beyond items).

Not for the old rule engine, but using the new rule engine, scripted automation, and the helper libraries, you can create variables in modules that can be accessed and changed in any of your scripts. You’d also be getting a head start preparing for OH 3.0. Let me know if you’d like some more details or help in getting things set up.

Looks like great potential, but also looks for a lot of work!

Will the old engine work in future too?

The new engine don’t looks like usable for every user :smiley:

Edit: Just read the docs of new Rule Engine. Missing: How to start to know whre to place files with scripts :slight_smile:

It’s easier to use than the rules DSL, and there is a way more documentation for Python.

There is no reason to have both, so at some point it will be removed. It’s too soon to tell if that will be OH 3.0. Be aware that the DSL is not the old rule engine, but it’s future is also TBD. There is still a lot to work out yet.

I’m not sure what you mean. The helper library documentation explains everything you need to install things, but it is much easier with the beta Jython bundle, which is the link in the previous post. Soon, it will be available to install through the UI.

Thanks for reply. I just overread that part.

So if I understand : unter etc/automation the scripts can still edited via ssh, right?

How will openhab work if some automation is done in .rules and some in new python?

My wife will kill me if something won’t work :joy:

Work just fine, because your various rules are “doing things” to the same objects - Items, things, events.

1 Like

Yes, just like when editing .rules files. I use VSCode and the SSH explorer.

Both rule engines can be used at the same time, so testing and migration is very easy to do. If you post a DSL rule, I’ll convert it for you to get you started.

What’s then minimum openhab2 Version to test?

For the beta Jython bundle? 2.5.0. For a manual install, you can go back to 2.4, but just upgrade to 2.5!

I installed everything ready now. Example Script worked and write to log every 10 s.

I think with the examples I can do a lot on my own. The Documentation in Scripts is not very well, but I think I understand.

A few Questions:

How to define a global Variable in Python? Should be done in right?
I want to define the Long values for Telegram ChatIDs there.

Didn’t find an example to define a long Value there. Could be some more in Example :wink:

Second question:

With Telegram Binding I have to define

val telegramAction = getActions(“telegram”, “telegram:telegramBot:homeid”)

in every old rule and then use

telegramAction.sendTelegram(12345678L, “TEXT”)

Can I define the Action in Init too?
Didn’t find an example for Telegramaction in Python

Same in Python:

t = actions.get("telegram", "telegram:telegramBot:openhab")
t.sendTelegram(chatid, msg)

Got it, thanks. Found some more examples while searching.

But went crazy with a simple if now.


if (Status_Home.state == 4) {


In Python

if items[“Status_Home”] == 4:

didn’t! Its just interpreted as false.

Got it on my own. Need to compare with DecimalType(4)

1 Like

Question again: I want to set variables for chat id telegram as global : how to do in python?

define in and import where you need it

Because I just started with testing python:

Question 1)
Define in /etc/openhab2/automation/lib/python/

or an new /etc/openhab2/automation/jsr232/python/personal/ ???

I allready tried to define in /etc/openhab2/automation/jsr232/python/personal/
like: ChatID = 12345678 thinking it will readed first and would work, but could to another place too.

Question 2)

Define like
ChatID=“12345678” there like port and host have been in example too?

Question 3)
in ==> Can you give me a Syntax to import? Thought configuration is allways readed and values can be used everywhere?

For people with more knowledge seems to be stupid questions :frowning: But I don’t find good overview with google how it works.