Journey to JSR223 Python 1 of 9

Now it works, thank you very much!

You should never use from core import actions as that will overwrite the built in actions object that you use to get binding actions. You should only ever use from core.actions import xxx and that only applies to about half a dozen core OH actions.

After my first experiments, there are still (at least) a couple of things that I don’t know how to implement in python. I hope this is the right place where to post these questions…

Here they are:

  1. How can I get the time of the last change of an Item’s state?

  2. How can I get the previous state of an Item?

I need 1) because I’m implementing a switch in the sitemap that will send an email containing information regarding all my anti theft Items, for example the current state of the entrance door and windows. I was thinking of sending an email with an HTML table with all those Items, their current state and the moment when they changed state last time (i.e. when the entrance door lock 1 was closed or opened last time).

I need 2) for a rule that will send a notification each time the overall power consumption of my house is above the allowed limit (basically the rule will be the following: if current_power_consumption > allowed_value and previous_power_consumption <= allowed_value then send notification).

Other things that I would like to know are the following:

  1. I read in an old post that the only way for having static values shared among rules is to create an Item and have it implement this behavior. Is this still valid with OH 3? Is there a different (simpler) method, for example in the case where the value must not be shared among different rules but only owned by a single rule (but preserving its value between subsequent calls of that rule)?

  2. If the only method for implementing a static shared variable is by creating an Item, I guess you can only associate basic types (numbers, strings, …) with this Item and in case you need to store more complex types (lists, dictionaries, …) you need to encode these values in a string and parse this string afterwards. Is this correct?

Thank you again!

  1. You’ve several options.

    • Persistence and use the Persistence Actions (note, depending on how this is configured it may give you the last update or it may not work at all if it’s using something like every minute as the persistence strategy).
    • Maintain the last changed times in a variable in the same file for both rules. One rule triggers when the Item(s) in question change and saves the current time in a variable. The second rule generates your email based on the variables.
    • Maintain the last changed in a separate DateTime Item using the timestamp Profile. Link the same Channel that populates the Item to a DateTime Item with the timestamp profile and it will save the date and time of the last change. No need for either a rule nor persistence.
  2. In what context? If you have a rule triggered by a changed event, the previous state is event.oldState. If you want the previous state other times, you need to set up persistence and then use the Persistence Actions to pull the previousState from the database. See Actions — openHAB Helper Libraries documentation for how to call the Persistence Actions in Python using the Helper Libraries.

  3. That has never been the case for Python and JavaScript. You can define static stuff in the library and then import it. In fact that’s the whole purpose of $OH_CONF/automation/lib/python/configuration.py (or configuration.js). That’s where (or a similar .py file in that folder) is where you should define static variables to be used by more than one rule. Static variables used only by one rule probably should be defined in that rule. And of course, any variable defined in the “global” context in a given .py file is available to any rule defined in that .py file. And since it’s defined outside the scope of any given rule, it keeps its value between runs of the rule. In JavaScript there are other tricks that can be used to preserve a variable between runs of a rule.

  4. If you were to store the data in an Item then yes, you’d use a String Item and parse the data. Or you can dynamically create and destroy Items on the fly in your rule. Or you can add the data as metadata to the Item.