Migrating my rules to Jython

MySQL used on a “remote” server. Which is local and connected wired and usually it is fast enough.
Persistence strategy is “everyChange” usually. And this is true for all of the items involved in this rule.
Also this rule is triggered by that item which I want to query, so in my opinion at least 1 data point should be available (which triggered) - however in reality more than that is available and can be seen on graphs also.
Tried adding a little sleep (0.1) and then a bigger sleep(0.5) before querying and that didn’t helped. However 3-4-5 minutes avarage works always (which should be slower in my opinion based on the more data points…).

Is the above preferable to the following?

if items["ChromecastAppName"].toString() != "Backdrop"

IMO, yes, since it helps to illustrate the type of the ChromecastAppName Item’s state rather generalizing it as a string. This is also a common pattern used for states where simple string conversion may not be possible, e.g. DecimalType(5), QuantityType(u"55 °F"), etc.

1 Like

I have most implementation in separate libraries, not the rule file and I have to explicitly import these.
In which module are these types/classes (e.g. StringType) defined implemented?
Is there any pointer to documentation about which OH objects re implemented in which module?
Is there an intelligent way to find out it myself?

it is for sure in core.*, but how to list all of them?

My understanding is these Types are referring to the actual Java classes and are not reimplemented in Scripted Automation or the Helper Libraries. To reference them in a module, you need to import them though so you need to use their Java package name.

In one of my modules I have:

from org.eclipse.smarthome.core.library.types import OnOffType, QuantityType, DecimalType, PercentType

I believe all of those classes will, if they haven’t already, move to org.openhab.core.library.types.

Eclipse Smarthome used to have a JavaDoc that was helpful to find stuff like this. I don’t think OH has that though so your best bet is to look in the openhab-core github repo I think. For the most part though it’s pretty easy to guess what the Type classes would be.

Lol… I think this is the third time I’ve answered this for you :slightly_smiling_face:. Scripts have several things added into the default script scope, which are documented here…

To use them in a module, use core.jsr223.scope to pull them in from the default script scope using something like this…

from core.jsr223.scope import StringType

Don’t be fooled into thinking you need to import these classes! Scripted automation builds a layer of abstraction between your rules, the rule engine, and OH. These class names have changed in OH 3.0. If you use them, this will break your rules when you upgrade and they could also very likely change again in the future. Soon, there will be even more helpful abstraction provided through a Scripting API, which will replace the core helper libraries.

1 Like

Just a small offtopic but related to my original post :slight_smile:
I have migrated almost all my rules to Python (only a few smaller left) and it was easier I thought.
Only had a few problems (documented above :slight_smile: ) anything except that worked like a charm.
Also it was a good time to rewrite my oldest rules…

It seems that it runs faster than DSL and loads correctly at startup, good to see the rules in PaperUI also…
Thanks for your work!

1 Like

This is true just for the stuff located in jsr223/python/personal and not for lib/python/personal.
For all my library modules I have to add explicit imports

What exactly are you explicitly importing? If it is in one of the presets, then it is not needed.

If I do not explicitly put

from core.jsr223.scope import StringType

in my library modules, pylint complains and at execution, I get an error:

2020-02-20 18:34:16.809 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/openhab/conf/automation/jsr223/python/personal/stopTimer.py': NameError: global name 'StringType' is not defined in <script> at line number 14

If I use the same in my rules fle pylint complains too, but the code executes.

2020-02-20 18:39:22.406 [INFO ] [jsr223.jythonstop.timer.log         ] - This a string type Hallo String

OK, thank you. Our terminology is not translating well :slightly_smiling_face:! What you are doing is safe and recommended. However, the following is what I was warning about and will cause you trouble in the future…

from org.eclipse.smarthome.core.library.types import StringType 

If you configure your .pylintrc properly, this will not occur.

[MESSAGES CONTROL]
disable=
    C,
    missing-docstring,
    unused-variable,
    line-too-long,
    logging-format-interpolation,
    bare-except,
    broad-except,
    pointless-string-statement,
    undefined-variable,
    unused-argument,
    wrong-import-position,
    anomalous-unicode-escape-in-string,
    global-statement

enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode

ignored-modules=
    org.joda.time,
    java,
    core,
    core.jsr223,
    core.actions,
    community,
    personal,
    configuration,
    events

[DESIGN]
# Maximum number of parents for a class (see R0901).
max-parents=15

what am I missing here

Use this…

[MESSAGES CONTROL]

enable=
    E,
    F,

    # VSCode defaults - https://code.visualstudio.com/docs/python/linting#_default-pylint-rules
    unreachable,
    duplicate-key,
    unnecessary-semicolon,
    global-variable-not-assigned,
    unused-variable,
    binary-op-exception,
    bad-format-string,
    anomalous-backslash-in-string,
    bad-open-mode

disable=
    C,
    R,
    reimported,
    missing-docstring,
    line-too-long,
    logging-format-interpolation,
    bare-except,
    broad-except,
    pointless-string-statement,
    unused-argument,
    wrong-import-position,
    anomalous-unicode-escape-in-string,
    global-statement,
    deprecated-method,
    dangerous-default-value,
    arguments-differ,
    redefined-builtin,
    redefined-outer-name,
    invalid-encoded-data

[TYPECHECK]

ignored-modules=
    com,
    java,
    javax,
    org,
    core,
    core.actions,
    core.jsr223,
    community,
    personal,
    configuration

[VARIABLES]

additional-builtins=
    reload,

    # Default Preset
    DateTime,
    LocalTime,
    State,
    Command,
    StringUtils,
    URLEncoder,
    FileUtils,
    FilenameUtils,
    File,
    UnDefType,
    NULL,
    UNDEF,
    IncreaseDecreaseType,
    DECREASE,
    INCREASE,
    OnOffType,
    ON,
    OFF,
    OpenClosedType,
    OPEN,
    CLOSED,
    StopMoveType,
    STOP,
    MOVE,
    UpDownType,
    UP,
    DOWN,
    RefreshType
    REFRESH,
    NextPreviousType,
    NEXT,
    PREVIOUS,
    PlayPauseType,
    PLAY,
    PAUSE,
    RewindFastforwardType,
    REWIND,
    FASTFORWARD,
    QuantityType,
    StringListType,
    RawType,
    DateTimeType,
    DecimalType,
    HSBType,
    PercentType,
    PointType,
    StringType,
    SIUnits,
    ImperialUnits,
    MetricPrefix,
    SmartHomeUnits,
    BinaryPrefix,
    items,
    ir,
    itemRegistry,
    things,
    rules,
    events,
    scriptExtension,
    se,

    # RuleSimple Preset
    ActionType,
    ConfigDescriptionParameter,
    ModuleType,
    SimpleActionHandler,
    SimpleConditionHandler,
    SimpleRule,
    SimpleTriggerHandler,
    TriggerType,
    Visibility,

    # RuleSupport Preset
    Action,
    ActionBuilder,
    Condition,
    ConditionBuilder,
    Configuration,
    ModuleBuilder,
    Rule,
    Trigger,
    TriggerBuilder,
    automationManager,
    ruleRegistry,

    # RuleFactories Preset
    ActionHandlerFactory,
    ConditionHandlerFactory,
    TriggerHandlerFactory
1 Like

The last few days a popup regarding “Visual Studio IntelliCode” was driving me nuts. So I clicked it away, unfortunately, this time with “ok”.

Now I cannot get rid of the old false messages in the error pane:

Any idea of what to revert. I uninstalled and installed both extensions:

Before uninstalling VSCode

Solved it by deleting the users settings.json, which had these entries:

{
    "window.zoomLevel": -1,
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "git.autofetch": true,
    "python.jediEnabled": false,
    "vsintellicode.python.completionsEnabled": false,
    "vsintellicode.features.python.deepLearning": "disabled",
    "python.autoUpdateLanguageServer": false
}

Though the popup reapeares :slight_smile:

“Later” Is my only option now!

Do not remember ever installed this Extension.

Unfortunately I’m still having problems with PersistanceExtensions. For small average it just returns null as I said before (however I’m pretty sure that there is more than one data persisted in this timeframe).

And now it stopped for other values as well. I just get ‘None’ for any average value I want to query. Persistance works, for example my charts in openHAB also use this persistance layer (mysql) and it can still draw them and I can access it manually… What could go wrong which makes this behavior?

I use mariadb and have not been able to reproduce this. It would help if you could distill it down to the simplest form.

I will try to set up a test scene. Anyway can I somehow update the RuleEngine? I saw that it will be available as a bundle soon, how could I migrate to that?
I still have the manually installed version…

Once it is available, you’ll just need to reverse the steps in the installation… shutdown OH, remove the lines from EXTRA_JAVA_OPTS, remove /automation/lib/python/core and /automation/lib/python/community, restart OH, and install the addons. Currently, I’ve separated the libraries, so there will be two addons, if everything is accepted… one for Jython and one for the helper libraries. I’ll be getting these out for beta testing soon, but I want to include the component scripts too.

Also I still have this problem and I don’t know how can I debug this either.
I have tried setting up the simplest test case.

Calling the PersistanceExtension service once to get an average of an item for the last 5 min and printing it to the log.
With one of my items, I always get None. The others work as it should, it prints the average of the last 5 min.
So it looks like there is no persisted data available. However when looking at a chart, there is, for example openHAB draws the chart for this item as well…