Correct… I know I’ve posted some examples with logs somewhere… here’s one. There is one difference with the Rules DSL that I’ve noticed, which people need to be aware of. Only one instance of a rule can run at a time, so the rule cannot be triggered again until the first instance completes. Just think of each rule as having its own threadpool with size of 1. Or that each rule has its own thread. I do not know if this should be considered a bug, as it has both good and bad side effects.
Make a rule with a JS Action that sleeps for 5s, then logs. Then trigger the rule quickly several times. There will be a 5s delay between each log.
from org.slf4j import Logger, LoggerFactory
from time import sleep
from openhab.rules import rule
from openhab.triggers import when
log = LoggerFactory.getLogger("org.eclipse.smarthome.model.script.Rules")
@rule("Test delay")
@when("Item Virtual_Switch_1 changed")
def testDelay(event):
log.debug("Test delay: start")
sleep(5)
log.debug("Test delay: stop")
2018-11-08 17:49:05.577 [DEBUG] [org.eclipse.smarthome.model.script.Rules] - Test delay: start
2018-11-08 17:49:10.578 [DEBUG] [org.eclipse.smarthome.model.script.Rules] - Test delay: stop
2018-11-08 17:49:10.579 [DEBUG] [org.eclipse.smarthome.model.script.Rules] - Test delay: start
2018-11-08 17:49:15.580 [DEBUG] [org.eclipse.smarthome.model.script.Rules] - Test delay: stop
2018-11-08 17:49:15.580 [DEBUG] [org.eclipse.smarthome.model.script.Rules] - Test delay: start
2018-11-08 17:49:20.580 [DEBUG] [org.eclipse.smarthome.model.script.Rules] - Test delay: stop
2018-11-08 17:49:20.581 [DEBUG] [org.eclipse.smarthome.model.script.Rules] - Test delay: start
2018-11-08 17:49:25.581 [DEBUG] [org.eclipse.smarthome.model.script.Rules] - Test delay: stop
2018-11-08 17:49:25.582 [DEBUG] [org.eclipse.smarthome.model.script.Rules] - Test delay: start
2018-11-08 17:49:30.582 [DEBUG] [org.eclipse.smarthome.model.script.Rules] - Test delay: stop
It should be possible to use globals in JSR223-JS, I just don’t know how to do it… or if they would be accessible through the JSON rule Actions. I just did some research on it, but I shut it down since I would prefer to put the effort into adding Jython scripts to JSON rule Actions.
I’m currently down and out with what appears to be a flu, so plenty of time to look at things… but not much comprehension possible, ATM. Although, I did have a thrilling conversation with some JS code earlier today! Coding fever dreams are my fave… makes them somehow slightly less trippy.
Templates are JSON. Here is a template with a JS Action that you can play with JS_Action_template.json (1.2 KB). (Wow… the forum takes .json files!). I couldn’t figure out what directory they are to be stored in. Copying to /conf/automation/templates/
did not cause them to show up. I ended up importing from a URL through Karaf (automation importTemplates http://localhost:8080/static/templates/JS_Action_template.json
). Of course, I copied the file there. After import, I could use them to create rules, but no files seemed to be created for the templates. Will be interesting after a reboot. Still learning… hope this helps.