Text string based on rule suddenly doesn't work anymore

Hello community!

I made a rule to show if the coffee is done or if the coffee machine is still running based on the power usage from the Fibaro Wall Plug which has power metering. If the power consumption is over 100 Watts, the text string, well, should say that the coffee making is in progress. If it is over 0 and under 100, the text says that the coffee is done, and if the value is 0, the text says that the machine is turned off.

I was happy when I got it to work, but suddenly the status doesn’t update anymore. I can still turn the power on, and the Watt metering value changes all the time, but the text string which is based on the number value doesn’t update. Can anyone see what’s wrong? I get no errors in the faillog, but I can’t see that the text string updates in the eventlog either. Last week, I could…

Items:

Switch Wallplug_Kaffetrakter_60m "Kaffetrakter timer" <poweroutlet> { expire="60m,command=OFF", channel="zwave:device:d67f18e5:node5:switch_binary" } // Timer 60 minutes
Number Wallplug_Kaffetrakter_Watt "Kaffetrakter Watt" <poweroutlet> { channel="zwave:device:d67f18e5:node5:meter_watts" } // Current Watt usage
String Wallplug_Kaffetrakter_Status // Status text line

Rules:

rule "Kaffetrakter rule"
when
        Item Wallplug_Kaffetrakter_Watt changed
then
        if(Wallplug_Kaffetrakter_Watt.state > 1 && Wallplug_Kaffetrakter_Watt.state < 100) postUpdate(Wallplug_Kaffetrakter_Status, "Kaffe klar, trakter på") // Text coffee ready
        else if(Wallplug_Kaffetrakter_Watt.state > 100) postUpdate(Wallplug_Kaffetrakter_Status, "Kaffe traktes") // Text coffee in the making
        else if(Wallplug_Kaffetrakter_Watt.state == 0) postUpdate(Wallplug_Kaffetrakter_Status, "Kaffetrakter avslått") // Text coffee machine off
        else if(Wallplug_Kaffetrakter_Watt.state == "") postUpdate(Wallplug_Kaffetrakter_Status, "ingenting") // Not in use
end

Sitemap:

        Frame label="Kaffetrakter" {
        Switch item=Wallplug_Kaffetrakter_60m label="Kaffetrakter, 1 time" // Activates the plug for 60 minutes
        Text item=Wallplug_Kaffetrakter_Status label="Status kaffetrakter [%s]" // Shows the string
        Text item=Wallplug_Kaffetrakter_Watt label="Effekt kaffetrakter" // Shows Watt
}

Log:

2017-05-08 22:47:07.862 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter changed from OFF to ON
2017-05-08 22:47:08.192 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_kWh changed from NULL to 13.14
2017-05-08 22:47:08.363 [ItemStateChangedEvent     ] - Kaffetrakter_watt changed from 0 to 418.1
2017-05-08 22:47:09.682 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 418.1 to 183.2
2017-05-08 22:47:13.661 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 183.2 to 155.3
2017-05-08 22:47:20.684 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 155.3 to 130.8
2017-05-08 22:47:26.681 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 130.8 to 106.8

As you can see from the log, the Watt value is updated and is visible in the sitemap, but the text string gets no information.

That looks as if the rule isn’t triggered, try to confirm that by using a logInfo in the rule This line should print in the log if the rule went to that line.

logInfo (“Kaffeetrakter”, " Rule is triggered by {}",Wallplug_Kaffeetrakter_Watt.state)

I’m pretty sure you have to use (Wallplug_Kaffetrakter_Watt.state as DecimalType) for each comparison as a state is not a number.

If you want to do a string comparison, you would have to use Wallplug_Kaffetrakter_Watt.state.toString.

You could test if Wallplug_Kaffetrakter_Watt.state does not belong to DecimalType this way:

if(!(Wallplug_Kaffetrakter_Watt.state instanceof DecimalType))

Thanks for the replies. I first tried the logInfo line (changed the text to Kaffetrakter, not Kaffee - Norwegian spelling). This doesn’t appear in the log.
I also tried adding “as DecimalType”, without any noticeable difference.
Example:

rule "Kaffetrakter rule"
when
	Item Wallplug_Kaffetrakter_Watt changed
then
	logInfo ("Kaffetrakter", " Rule is triggered by {}",Wallplug_Kaffetrakter_Watt.state)
	if(Wallplug_Kaffetrakter_Watt.state as DecimalType > 1 && Wallplug_Kaffetrakter_Watt.state as DecimalType < 100) postUpdate(Wallplug_Kaffetrakter_Status, "Kaffe klar, trakter på")
	else if(Wallplug_Kaffetrakter_Watt.state as DecimalType > 100) postUpdate(Wallplug_Kaffetrakter_Status, "Kaffe traktes")
	else if(Wallplug_Kaffetrakter_Watt.state as DecimalType == 0) postUpdate(Wallplug_Kaffetrakter_Status, "Kaffetrakter avslått")
	else if(Wallplug_Kaffetrakter_Watt.state as DecimalType == "") postUpdate(Wallplug_Kaffetrakter_Status, "ingenting")
end

I then added a new rule to see if I could update the Wallplug_Kaffetrakter_Status state another way;

rule "Kaffetrakter status test"
when
	Item Wallplug_Kaffetrakter_60m changed
then
	if(Wallplug_Kaffetrakter_60m.state == OFF) postUpdate(Wallplug_Kaffetrakter_Status, "switch off")
	else if(Wallplug_Kaffetrakter_60m.state == ON) postUpdate(Wallplug_Kaffetrakter_Status, "switch on")
end	

Now something strange happens! Both values updates - in some way.
Both the wanted text string “Kaffetrakter avslått” and the test text “switch off” activates when the switch is disabled. The values for on (brewing in progress and coffee ready) however, does not update.

2017-05-09 22:39:43.671 [ItemCommandEvent          ] - Item 'Wallplug_Kaffetrakter_60m' received command ON
2017-05-09 22:39:43.697 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_60m changed from OFF to ON
2017-05-09 22:39:44.481 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 0 to 451.7
2017-05-09 22:39:45.483 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 451.7 to 168.6
2017-05-09 22:39:47.486 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 168.6 to 139.4
2017-05-09 22:39:49.067 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Status changed from Kaffetrakter avslått to switch on
2017-05-09 22:39:52.470 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 139.4 to 99.2
2017-05-09 22:39:53.465 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 99.2 to 114.5
2017-05-09 22:40:15.466 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 114.5 to 86.5
2017-05-09 22:40:24.056 [ItemCommandEvent          ] - Item 'Wallplug_Kaffetrakter_60m' received command OFF
2017-05-09 22:40:24.083 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_60m changed from ON to OFF
2017-05-09 22:40:24.121 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Status changed from switch on to switch off
2017-05-09 22:40:24.863 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Watt changed from 86.5 to 0
2017-05-09 22:40:24.907 [ItemStateChangedEvent     ] - Wallplug_Kaffetrakter_Status changed from switch off to Kaffetrakter avslått

Confused! Why did this work perfectly for many weeks and now not?

You forgot the brackets :wink:
But I did a short test with SmartHome Designer and would suggest the following version:

var int iWatt = 0  // at the very beginning of the rule file


rule "Kaffetrakter"
when
    Item Wallplug_Kaffetrakter_Watt changed
then
    if(!(Wallplug_Kaffetrakter_Watt.state instanceof DecimalType))
        Wallplug_Kaffetrakter_Status.postUpdate("ingenting")
    else {
        iWatt = (Wallplug_Kaffetrakter_Watt.state as DecimalType).intValue
        if(iWatt > 0 && iWatt < 100) 
            Wallplug_Kaffetrakter_Status.postUpdate("Kaffe klar, trakter på")
        else if(iWatt >= 100) 
            Wallplug_Kaffetrakter_Status.postUpdate("Kaffe traktes")
        else if(iWatt == 0) 
            Wallplug_Kaffetrakter_Status.postUpdate("Kaffetrakter avslått")
    }
end

I used a var iWatt as the correct term is very long, but you could use the complete term instead of the var either.

As you can see, the first thing is, to proove the Value of Wallplug_Kaffetrakter_Watt.state is of type DecimalType. If not, this will result in a state "ingenting"
Otherwise, the var is set to the actual state as an integer.
After that, the comparison is way more easy. :wink:
Please notice that I changed the values to avoid gaps.

Personally, I prefer the Method over the Action, but this is not relevant in this case (Action always needs a string, which will sometimes give strange errors…).

Thanks Udo, this worked!

I probably learned something new as well. :slight_smile:

Can you explain Method vs Action? Is it the placement of the postUpdate command?

Method:

MyItem.postUpdate(Value)  //change Value of Item without sending a command
MyItem.sendCommand(Value) //send command to bound devices

Action:

postUpdate(MyItem,Value)  //change Value of Item without sending a command
sendCommand(MyItem,Value) //send command to bound devices