Rule: java.lang.String ERROR

  • Platform information:
    • Hardware: Android Samsung Galaxy S5 Neo
    • OS: with LinuxDeploy and installed Ubuntu
    • Java Runtime Environment: 1.8.0_144
    • openHAB version: 2.5.6 stable

Hi,

I have a working rule and I’m trying to clean my log of WARN and ERROR messages. The following rule is working but generating one ERROR line in this actual state:

Item:
DateTime EchoWohnzimmer_NaechsterTimer "EchoWohnzimmer_NaechsterTimer" {channel="amazonechocontrol:echo:<Echo ID>:nextTimer"}

Rule:
import java.lang.String
var Timer echotimer

    rule "Echo-Timer"

    when
        Item EchoWohnzimmer_NaechsterTimer changed
    then
            // Timer Date and time 2019-10-20T21:58:35.870-0600
            val String timerString = EchoWohnzimmer_NaechsterTimer.state.format("%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS")

    echotimer = createTimer(now, [ |

            // parse String to Date Time
            var DateTime timestring = parse(timerString)

            // find out time left in minutes
            val timeleft = (((timestring.millis + 60000) - now.millis) / 60000)

            // finish sequence if the time left is 0
            if(timeleft == 0) {
                EchoTimer.sendCommand("Timer abgelaufen")
                Thread::sleep(2000)
                EchoTimer.sendCommand("Kein aktiver Timer")
            }

                else {
                    // update the timer Item with time remaining 
                    EchoTimer.sendCommand("noch " + timeleft + " Min.")
                    // reschedule the timer for 1 minute
                    echotimer.reschedule(now.plusMillis(60000))
                }
        ] )

    end

    rule "Echo-Timer abgebrochen"
    when 
        Item EchoWohnzimmer_NaechsterTimer changed to UNDEF
    then
        EchoTimer.sendCommand("Kein aktiver Timer")
    end

And the Openhab.log show this ERROR:
2020-07-10 12:11:50.551 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Echo-Timer': Y != java.lang.String

Seems to be a problem with the DateTime format of EchoWohnzimmer_NaechsterTimer.state.format("%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS") and especially the Year

Thank’s for any help!

You don’t need that import

You’re anticipating UNDEF sometimes, that’s good.

but the other rule will still run as well …

I don’t think that format is going to work out with UNDEF, as UNDEF is not a string. You might test for that state first.

Would allow you to roll both rules into one, in fact.

Ah ok, you mean the problem occurs in the step of item is changing to UNDEF, which triggers the rule with timestamp format. That wasn’t in my focus yet.

I tried to implement it into one rule, but the comparison with == UNDEF wasn’t working properly. I devided into 2 different rules which work then. The UNDEF is needed, if the timer is aborted before it is counted to 0, to trigger the indicator item in HABPanel.

Is it possible to change the rule trigger of the first one to e.g.

when
        Item EchoWohnzimmer_NaechsterTimer changed from UNDEF to <???>

I guess this should fix it, if possible?

No, but you get that effect with

when
        Item EchoWohnzimmer_NaechsterTimer changed from UNDEF

but that might not be what you want. That rule would only ever run when the Item changed to UNDEF and then something else. It would not run if the Item just changed from xx to yy in the ordinary way.

Can’t see what you did, can’t fix it.
Guessing; you forgot .state, we all do that.

if (someItem.state == UNDEF) {
   ...
} else {
   ...
}

Hi rossko,
thank you for the immediate great support. The fusion to one rule with the item.state == UNDEF was the solution!

So, final working rule without annoying ERROR message is:

rule "Echo-Timer"`
when
    Item EchoWohnzimmer_NaechsterTimer changed
then
    if (EchoWohnzimmer_NaechsterTimer.state == UNDEF) {
    EchoTimer.sendCommand("Kein aktiver Timer")
    }
        else {
            // Timer Date and time 2019-10-20T21:58:35.870-0600
            val timerString = EchoWohnzimmer_NaechsterTimer.state.format("%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS")

                echotimer = createTimer(now, [ |
                // parse String to Date Time
                var DateTime timestring = parse(timerString)
                // find out time left in minutes
                val timeleft = (((timestring.millis + 60000) - now.millis) / 60000)

                        // finish sequence if the time left is 0
                        if(timeleft == 0) {
                        EchoTimer.sendCommand("Timer abgelaufen")
                        Thread::sleep(2000)
                        EchoTimer.sendCommand("Kein aktiver Timer")
                        }
                            else {
                            // update the timer Item with time remaining 
                            EchoTimer.sendCommand("noch " + timeleft + " Min.")
                            // reschedule the timer for 1 minute
                            echotimer.reschedule(now.plusMillis(60000))
                            }
                    ] )
            }
    end

If interesting for someone, who will read this post, this rule is used to show the left minutes of an Amazon Echo timer in the item “EchoTimer”, which is displayed in the HABpanel. Therefor the channel “nexttimer” of the amazon echo binding is used and linked to the item “EchoWohnzimmer_NaechsterTimer”. If you set more than one timer, the shortes left time is displayed in this EchoTimer item. If the shortest time is counted to 0 it will show the next shortes left time of the other existing timer.

Great forum and awsome member which helps a lot of using the OpenHAB platform.

I’m pretty sure that this code is not working as suggested. timerString is a val defined in a rule, so when the timer succeeds, there is no timerString at all. Instead you have to use a global var.
Another thing: why using a Thread::sleep(2000) this is ugly code.

I was missing to mention the global
var Timer echotimer

But, it is working properly and in daily use.
This was a result from web research, copy and pasting and adapting with own item names.
Ugly code… ok. Could be, because i’m not a prof. programmer and was using existing code as base, but it works.