My first (failed) attempt at Jython for a couple of rules. Help please. I am on 2.5M4
Item
Switch IsItDark <moon>
Script
from core.rules import rule
from core.triggers import when
from core.jsr223.scope import events
@rule("Jython IsItDark Sunset)", description="This triggers the IsItDark Switch based on sunset time")
@when("Channel astro:sun:local:set#event triggered START")
events.sendCommand(IsItDark, "ON")
@rule("Jython IsItDark Sunrise)", description="This triggers the IsItDark Switch based on sunrise time")
@when("Channel astro:sun:local:rise#event triggered START")
events.sendCommand(IsItDark, "OFF")
Error
2019-10-20 19:19:25.892 [INFO ] [me.core.service.AbstractWatchService] - Loading script 'python/personal/IsItDark.py'
2019-10-20 19:19:27.345 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/python/personal/IsItDark.py': SyntaxError: mismatched input 'events' expecting CLASS in <script> at line number 7 at column number 0
My Rules DSL
rule "Sunset"
when
Channel 'astro:sun:local:set#event' triggered START
then
IsItDark.postUpdate(ON)
end
rule "Sunrise"
when
Channel 'astro:sun:local:rise#event' triggered START
then
IsItDark.postUpdate(OFF)
end
You donāt need to import this in scripts, since āeventsā is automatically imported for you in the default script scope.
What Rich said, with some consolidationā¦
from core.rules import rule
from core.triggers import when
@rule("Jython IsItDark)", description="This triggers the IsItDark Switch based on sunrise and sunset times")
@when("Channel astro:sun:local:set#event triggered START")
@when("Channel astro:sun:local:rise#event triggered START"
def is_it_dark(event):
events.sendCommand(IsItDark, "ON" if "set" in str(event.channel).split(":") else "OFF")
I have not used decorators before in Python. Is i OK to use this thread as I develop my other rules? I am thinking of another case where I think 4 rules could become one.
Basically I am migrating from an M3 Pi system to an M4 VM system with a better Z-Wave controller. I figured I could migrate the rules to Jython & then actually move the Things to minimize service disruption. This rule is just the first piece but I only have 2 main services & a third planned currently.
I donāt have a problem with it. Though there are two schools of thought. One topic per thread makes it easier for others to find. But sometimes seeing a narrative is also useful.
Thanks. I asked in another thread how to get & use your Expire library.
With those two I should be able to code rules for an outdoor light / motion sensor only working if it is dark & turning off 5 minutes after last motion detected.
It would take minutes to copy a few files, customize the configuration, setup your Items and groups, and then youād be done and all setup to easily add more automations in the future. IMO, building everything from scratch is overkill ! Let us know how that new wheel rolls!
One thing I will say about Expire with Jython Rules. The benefits of Expire Based Timers pretty much disappear in Python Rules. So if you are mainly using Expire instead of Timers, you will probably be better off going to use Timers instead.
If you have situations where you have one Timer per Item, look at the Timer Manager PR I submitted to the Helper Library as well. It handles managing those for you pretty easily.
It does take a good deal of time and effort to figure out how something like that works. At first glance, I could see how a number of users would say to themselves āI donāt need all that, itāll be easier to just do it myself.ā I see it all the time with even the Rules DSL version of Time of Day.
You and I both know that itās definitely worth the little bit of effort up front to learn and use libraries like that, but lots of OH users donāt have the benefit of our experience.
I am still taking baby steps here. This is the DSL I am trying to eliminate next with MyTimer being a 5 minute expiry timer.
rule "Papillon Motion On"
when
Item papillon_motion changed from OFF to ON
then
if (IsItDark.state == ON) {
papillon_light.sendCommand(ON)
// start Timer
MyTimer.sendCommand(ON)
}
end
rule "Papillon Motion Off"
when
Item MyTimer changed from ON to OFF
then
papillon_light.sendCommand(OFF)
end
Iām just trying to turn you around as you toddle off in a direction that will be more effort . The area trigger code handles all of this for youā¦ the timers, mode (ToD) and lux are built in.
For this last one, add the Items to groups and then set some metadata (doesnāt look like youāre using a lux sensor)ā¦
The day & night in the astro binding did not look suitable.
Perhaps I can digest your code later., as I test it.
When I tested your code earlier today, I understood the logic behind it.
Iāll tell you whatā¦ post your DSL rules and the Items, and I will setup your groups and metadata. It will be useful for others too. Or notā¦ I donāt want to sound pushyā¦ whatever works for you!