Simple temperature changing rule

Hello,

I´m trying to set up a simple rule when temperature is changing, but all I get is an error message in the logfile

“[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘SaunaTemp’: An error occurred during the script execution: index=1, size=1”

I already searched for this kind of error, but didn´t get it resolved.
Also checked all brackets with Notepad++ and realized that the bracket behind the number 70.0 stays black. (usually the corresponding bracket reverts red and is displayed in the editor). Could this be the reason?
What I´m doing wrong and how do I fix it?

    rule "SaunaTemp"

when
	Item SaunaTemp changed

then
    if (Double::parseDouble(SaunaTemp.state.toString) > 70.0) {
       logInfo("SaunaTemp above 70")
              }
else if (Double::parseDouble(SaunaTemp.state.toString) < 70.0) {
       logInfo("SaunaTemp under 70")
       }
end

The error is due to logInfo requiring two arguments, so you’d use something like…

logInfo("Sauna", "SaunaTemp above 70")

Getting fancy…

logInfo("Sauna", "SaunaTemp above 70: {}", newState)

This may get you off into the weeds, but you also do not need the conversion to double. Assuming SaunaTemp is reporting QuantityType…

if (SaunaTemp.getStateAs(QuantityType) > 70|°F) {

Thanks! I´ve updated the rule; no errors any more, but als nothing happens.

This is what I see in the logfile when channel is updating:

2020-12-04 10:28:42.294 [DEBUG] [ding.zwave.handler.ZWaveThingHandler] - NODE 38: Updating channel state zwave:device:95603ab1:node38:sensor_temperature3 to 70.56 °C [QuantityType]

And this is my rule now:

rule "SaunaTemp"

when
	Item SaunaTemp changed

then
 if (SaunaTemp.getStateAs(QuantityType) < 70|°C) {
   logInfo("Sauna", "Sauna under 70")
          }
			  
end

I´ve als tried following; just removed “|°C” and now I get logInfo at EVERY change. more or less than 70…
this is wired :frowning:

70.56|°C > 70|°C is true, so I’d expect nothing to happen in your rule. What happened to the the half :slightly_smiling_face:? Did you remove the other rule? If not, you would have two rules with the same name, which would make one not execute. When troubleshooting a rule, logging is one of the best tools. Try this…

rule "SaunaTemp test"

when
    Item SaunaTemp changed
then
    logWarn("Test", "SaunaTemp: {}, SaunaTemp.getStateAs(QuantityType) < 70|°C: {}", SaunaTemp.state, SaunaTemp.getStateAs(QuantityType) < 70|°C)
    if (SaunaTemp.getStateAs(QuantityType) < 70|°C) {
        logInfo("Sauna", "Sauna under 70")
    } else {
        logInfo("Sauna", "Sauna equal to or above 70")
    }
end

You must compare QuantityType to QuantiityType. You can use a conversion, but there is no need to throw out data that might be useful. Comparisons between QuantityType and Number are always true, which should really be entered as a bug.

Thanks for your help, Scott.

I´ve just tested your code and got following down below.
(I replaced 70 with 22 for testing current temp values )

so what I do not understand: It always says “…equal to or above 22”, but it definitely lower than 22.
and the other question: how to send the message just one time, e.g. when 22 is reached, and not at every update at values above of 22.

 ||79151|2020-12-04 16:17:18.966 [WARN ] [.eclipse.smarthome.model.script.Test] - SaunaTemp: 23.87, SaunaTemp.getStateAs(QuantityType) < 22|°C: false|
    |---|---|---|
    ||79152|2020-12-04 16:17:18.967 [INFO ] [eclipse.smarthome.model.script.Sauna] - Sauna equal to or above 22|
    ||79174|2020-12-04 16:18:00.974 [WARN ] [.eclipse.smarthome.model.script.Test] - SaunaTemp: 22.93, SaunaTemp.getStateAs(QuantityType) < 22|°C: false|
    ||79175|2020-12-04 16:18:00.976 [INFO ] [eclipse.smarthome.model.script.Sauna] - Sauna equal to or above 22|
    ||79197|2020-12-04 16:18:42.977 [WARN ] [.eclipse.smarthome.model.script.Test] - SaunaTemp: 22.18, SaunaTemp.getStateAs(QuantityType) < 22|°C: false|
    ||79198|2020-12-04 16:18:42.979 [INFO ] [eclipse.smarthome.model.script.Sauna] - Sauna equal to or above 22|
    ||79221|2020-12-04 16:19:24.978 [WARN ] [.eclipse.smarthome.model.script.Test] - SaunaTemp: 21.68, SaunaTemp.getStateAs(QuantityType) < 22|°C: false|
    ||79222|2020-12-04 16:19:24.979 [INFO ] [eclipse.smarthome.model.script.Sauna] - Sauna equal to or above 22|
    ||79246|2020-12-04 16:20:27.980 [WARN ] [.eclipse.smarthome.model.script.Test] - SaunaTemp: 21.18, SaunaTemp.getStateAs(QuantityType) < 22|°C: false|
    ||79247|2020-12-04 16:20:27.981 [INFO ] [eclipse.smarthome.model.script.Sauna] - Sauna equal to or above 22|
    ||79359|2020-12-04 16:21:09.984 [WARN ] [.eclipse.smarthome.model.script.Test] - SaunaTemp: 20.68, SaunaTemp.getStateAs(QuantityType) < 22|°C: false|
    ||79360|2020-12-04 16:21:09.986 [INFO ] [eclipse.smarthome.model.script.Sauna] - Sauna equal to or above 22|

OK, so we got out into the weeds :slightly_smiling_face:. The log shows that SaunaTemp is not using QuantityType (note the state of SaunaTemp is just a plain number without unit). Either change your Item definition to use Number:Temperature or just use…

rule "SaunaTemp test"

when
    Item SaunaTemp changed
then
    if (SaunaTemp.state < 22) {
        logInfo("Sauna", "Sauna under 22")
    } else {
        logInfo("Sauna", "Sauna equal to or above 22")
    }
end

Again, many thanks for your help, Scott!
I think I´ve managed to work it now.

Will test it in “real” next days :slight_smile:

> var boolean Notify = false
> 
> rule "SaunaTemp"
> 
> when
> 	Item SaunaTemp changed
> 
> then
>      if ((SaunaTemp.state) > 70 && Notify == false ) {
>        {sendNotification("email@email", "Sauna Temperature:  " + SaunaTemp.state + " C")
> 	   sendCommand(DeviceNotifyInfo, "Sauna Temperature:  " + SaunaTemp.state + " C")
> 	   Notify = true}
>               }
> 
> else if ((SaunaTemp.state) < 30  && Notify == true ) {
>        {sendNotification("email@email", "Sauna switched OFF:  " + SaunaTemp.state + " C")
> 	   sendCommand(DeviceNotifyInfo, "Sauna switched OFF:  " + SaunaTemp.state + " C")
> 	   Notify = false} 
>               }
> 			  
> 		  
> end

You are very welcome! QuantityTypes are very useful when you need them. In your new rule, it looks like you have some extra left curly brackets, and you may need to use SaunaTemp.state.toString in a few places where you are concatenating strings.