JSR223 Jython - Converting from Rules DSL

Hi

I"m in the process of converting my rules from the Rules DSL to JSR223 usimng jython and have a couplde of things I can’t solve…

Firstly, I’m trying to convert this rule but can’t work out how to get the day of the week

ule "Packed Lunch Reminder"
    when
        Time cron "0 0 22 * * ? *"
    then
        var Number DayOfWeek = now.getDayOfWeek
        if (DayOfWeek == 2) {
            ECHOFRONTROOM_TTS.sendCommand("Good evening, don't forget that Joseph has packed lunch tomorrow")
        }
    end

The second thing is how to print a item state or variable value in a log message?

Assuming you are using the helper libraries…

from core.rules import rule
from core.triggers import when

@rule("Packed Lunch Reminder")
@when("Time cron 0 0 22 * * ? *")
def packed_lunch_reminder(event):
    from org.joda.time import DateTime
    day_of_week = DateTime.now().getDayOfWeek()
    if day_of_week == 2:
        events.sendCommand("ECHOFRONTROOM_TTS", "Good evening, don't forget that Joseph has packed lunch tomorrow")

You’ll need to import Joda DateTime and then you can use the same methods you used in the DSL.

Logging is documented here… https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Logging.html and Item states are documented here… https://openhab-scripters.github.io/openhab-helper-libraries/Guides/But%20How%20Do%20I.html#get-the-state-of-an-item. Come back if you need more detail or help!

I was trying to use java time rather than Joda as I understood it will be deprecated/removed at some point.

Joda is deprecated, but it won’t be removed from OH for a while. And you can always add it as a lib when using scripted automation. But if you prefer not to use Joda, just use Python…

from core.rules import rule
from core.triggers import when

@rule("Packed Lunch Reminder")
@when("Time cron 0 0 22 * * ? *")
def packed_lunch_reminder(event):
    from datetime import date
    day_of_week = date.today().weekday()
    if day_of_week == 1:# Monday is 0
        events.sendCommand("ECHOFRONTROOM_TTS", "Good evening, don't forget that Joseph has packed lunch tomorrow")

3 posts were split to a new topic: Help converting rule to jython