Issues with DateTime in OpenHab 2

Hi all,

I am having issues with my rule and need help.

for some reason I can’t get the code below to work. I am building an irrigation system and want to define startTime but can’t get it working.

Can you help?

rule "Irrigation run"
Time cron "0 0 0 * * ?"
if (Irrigation_Master.state == ON) {
// get the scale factor - used to reduce the run times across the board
var Number scaleFactor = Irrigation_ScaleFactor.state as DecimalType

        // convert our start time to a joda.time.DateTime for today     
        var DateTime startTime = parse(now.getYear()+"-"+now.getMonthOfYear() + "-" + now.getDayOfMonth() + "T" + "13:00" + ":00")
        var DateTime endTime

        // get the raw run times for each zone (in mins)
        var Number lawnMins = Irrigation_LawnMins.state as DecimalType
        var Number vegeMins = Irrigation_VegeMins.state as DecimalType
        var Number backMins = Irrigation_BackMins.state as DecimalType
        var Number frontMins = Irrigation_FrontMins.state as DecimalType

        // convert to the actual run times (by applying the scale factor)
        var int lawnTime = ((lawnMins * scaleFactor) / 100).intValue
        var int vegeTime = ((vegeMins * scaleFactor) / 100).intValue
        var int backTime = ((backMins * scaleFactor) / 100).intValue
        var int frontTime = ((frontMins * scaleFactor) / 100).intValue

        // turn on each zone in turn (with a minute gap between each zone activation)
        if (lawnTime > 0) {
            endTime = startTime.plusMinutes(lawnTime)
            createTimer(startTime) [| sendCommand(Irrigation_Lawn, ON) ]
            createTimer(endTime) [| sendCommand(Irrigation_Lawn, OFF) ]
            startTime = endTime.plusMinutes(1)

        if (vegeTime > 0) {
            endTime = startTime.plusMinutes(vegeTime)
            createTimer(startTime) [| sendCommand(Irrigation_Vege, ON) ]
            createTimer(endTime) [| sendCommand(Irrigation_Vege, OFF) ]         
            startTime = endTime.plusMinutes(1)

        if (backTime > 0) {
            endTime = startTime.plusMinutes(backTime)
            createTimer(startTime) [| sendCommand(Irrigation_Back, ON) ]
            createTimer(endTime) [| sendCommand(Irrigation_Back, OFF) ]
            startTime = endTime.plusMinutes(1)

        if (frontTime > 0) {
            endTime = startTime.plusMinutes(frontTime)
            createTimer(startTime) [| sendCommand(Irrigation_Front, ON) ]
            createTimer(endTime) [| sendCommand(Irrigation_Front, OFF) ]
            startTime = endTime.plusMinutes(1)

I’m not sure if this is different in OH 2 or if you imported something deeply, but typically you need to call DateTime::parse, not just parse.


I have also spend a lot of time on this same rule (many evenings for the last 2 months to try and get it working correctly - I use Setpoint items to assign the start time and duration times). I am still experiencing random results, but have made several improvements during my experimentation. I list a few comments, but not sure which improved the situation - but looks like most of the below-listed actions have had some positive effect.

  1. I recently upgraded to OH 2.0.0b3, I think b2 was less stable.

  2. I also loaded the latest Raspbian Jesse (2016.05.10) - I am using Raspberry Pi 2B.

Before this, I was using an older version of Jesse and OH 1.8.2 or 1.8.3, cannot remember - I had endless issues with the rules, also GPIO.

  1. The OH2 posts state that we do not need to import libraries into the rules file for OH2, but DateTime/Timer did not fail again after I added the following imports to the rules file:
    import org.joda.time.DateTime.*
    import org.openhab.model.script.actions.Timer

EDIT: My latest experimentation is without the above imports in OH2, and it is working fine.

  1. The irrigation rules use startTime and endTime as variables - I changed these to startTime1, endTime1 etc… I suspect that startTime and endTime may conflict somewhere else in the library or OH deployment. Try to experiment with this; I changed the first two sprinkler zones and left the last two with the original startTime and endTime. The zones that were changed work well now, but the rules fail at the unchanged fields. I will fix this soon, but still experimenting.

  2. The parse(now…) works well for me, and did not need any specific changes.

  3. I still have issues with now.plusMinutes, now.plusHours, etc. where it keeps responding in a random way. The main issue is with assignment of non-integers. I even tried .intVal and various other methods of changing the DecimalType to an Integer - this does not work either. Using now.plusMinutes(5) for example works well, but randomly fails when assigning a variable to it. I am not a Java developer, so rely a lot on experimentation and testing the results (fault finding). I will post a ‘request for help’ on this topic and explain my issues and findings so far. The weird thing is that it sometimes works, and then just stops working, after which the rules keep failing in the log file with reason now.plusMinutes(int).

I hope these inputs help.

mine is working now. It was issue with the designer. Download the eclipse smart home designer

i think will work.