Design Pattern: Time Of Day

Your problems have nothing to do with the rule examples this thread is about.

This line, as a “global” definition -

populates your CurrentTime variable with a value only once, at rules file load time.
So far as I can see, you never update it, so yes it will stay the same.
This behaviour has not changed since OH2.
Wouldn’t you be interested in “now” at the time the rule runs?

When you’re ready, you might show us those.

I don’t know what your Zwave problems are about, but not related to this rule.

But it isn’t. It’s populated when the xxx.rules file is loaded. You’ve placed it outside of your rule.

See also

Quick question.
Would an Item that is already defined with { etod="MORNING"[type="default"] } be the same as the API where:

Namespace = etod

{
  "value": "MORNING",
  "config": {
    "type": "default"
  }
}

? One of my day-types isn’t happening so I wonder if it is the metadata that is wrong and would like to update it via the api…

That metadata doesn’t look wrong but it’s hard to tell by just looking at the JSON like that.

If the metadata is present but not well formed then the rule will complain about that.

You can see which Items are being considered by the rule by turning up the logging to DEBUG when it runs. It will list all the Items it’s using for the day.

If you don’t get any errors and you don’t see your Item in the debug logs then for some reason the rule doesn’t see that Item at all. If you see errors then the error should tell you what’s wrong.

Oh I get it now… thanks :slight_smile:

I’m trying to get the javascript version running on my OH3.1 system. But when executing the script imported from ephemTimeOfDay.yml io get this error in openhab.log:

2021-07-18 11:23:15.674 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '1d2a859923' failed: org.graalvm.polyglot.PolyglotException: ReferenceError: "scriptExtension" is not defined

I cleaned /etc/openhab/automation before installing all files mentioned in the README.md at:

since i got some python and javascript fragments there, from trying some older OH2.5 examples. When looking at the javascript examples at:

it looks like i’m missing some really basic thing here since

scriptExtension.importPreset("default");

is getting used there frequently and without mentioning any requirements?

I’m using the official .deb package on a Ubuntu 20 system if that matters…

regards,

Christoph

Create a simple rule with a JavaScript script action. Just have it log something. Does that generate the same error?

This is something very fundamental wrong and not specific to this role I think. But if the error occurs even on a simple rule maybe not.

Logging into Karaf, would this line to the job? log:set DEBUG openhab.model.script.Rules.TimeOfDay

I still get no debug log, only info.

You’re missing the “org” part of the logger name.

org.openhab.model.script.Rules.TimeOfDay

Thanks for the reply - i’ve created this javascript script and it works:

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Examples");

logger.info("Typing needed classes");

adding a line with:

scriptExtension.importPreset("RuleSupport");

raises the “ReferenceError” exception…

You are using the built in ECMAScript 5.1, not the GraalVM JavaScript addon? The above only works with the default JavaScript.

How do i select using the “default JavaScript”? When creating a script or rule i only have these options:

I tried the Blockly option where i switched to the code view and pasted the code and the ECMAScript option as well…

It appears that if you’ve installed the GraalVM JavaScript addon it replaces the default one. In other words, you can’t. If you do not plan on using the GraalVM JavaScript, don’t install it. If you do plan on using it you’ll have to rewrite the rule above to work with that.

Thank you for clarifying! After uninstalling the GraalVM addon and restarting openhab, the script is working finally!

You’ve also installed Jython and Groovy too. Do you have plans to use those? If not don’t install them. The more stuff you have installed the more opportunities there are for problems. Only install add-ons (and software for that matter) you intend to use. Don’t install stuff “just in case”.

Good advice - i will do so.

Logging debug-info worked and one “Time of day”-item was indeed missing. Instead of messing about with metadata in the API-explorer, I just created the item again with info from your github. Now it works fine. Thanks

I migrated from installation from armhf to arm64 today, and restored from backup. The java version is

$ java -version
openjdk version “16.0.2” 2021-07-20
OpenJDK Runtime Environment Zulu16.32+15-CA (build 16.0.2+7)
OpenJDK 64-Bit Server VM Zulu16.32+15-CA (build 16.0.2+7, mixed mode)

apt search zulu|grep installed

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

zulu-repo/now 1.0.0-2 all [installed,local]
zulu16-ca-doc/stable,now 16.0.2-1 arm64 [installed,automatic]
zulu16-ca-jdk/stable,now 16.0.2-1 arm64 [installed,automatic]
zulu16-ca-jdk-headless/stable,now 16.0.2-1 arm64 [installed,automatic]
zulu16-ca-jre/stable,now 16.0.2-1 arm64 [installed,automatic]
zulu16-ca-jre-headless/stable,now 16.0.2-1 arm64 [installed,automatic]
zulu16-doc/stable,now 16.0.2-1 arm64 [installed,automatic]
zulu16-jdk/stable,now 16.0.2-1 arm64 [installed]
zulu16-jdk-headless/stable,now 16.0.2-1 arm64 [installed,automatic]
zulu16-jre/stable,now 16.0.2-1 arm64 [installed,automatic]
zulu16-jre-headless/stable,now 16.0.2-1 arm64 [installed,automatic]

Every other rules seems to be ok except, the ones required here.

For ephemTimeOfDay I get,

INVALID_RULE
Validation of rule 1bed65e3c8 has failed! Action Type “script.ScriptAction” does not exist!

Also my dsl rules show up as invalid. Any idea what could be the issue?

Does this have anything to do with ‘Time of Day’?

You’ve ignored the Java 11 requirement, assuming you are using OH3

That’s probably the reason, yes.
I have:
zulu-11-azure-jdk/stable,now 11.50+19-1

Note the dash after “zulu”. I do have a v16 in my repository, but that’s called:

zulu-16-azure-jdk/stable 16.32+15-1