[SOLVED] Rule: Compare state of item with IF/Else not working

Hey guys,

I am currently trying to compare the state of a Number Item within a rule as a trigger for an IF-Statement. Unfortunately, I can’t get it to work. I tried several things, but nothing worked correct. The current problem right now is, the rule always triggers the IF-Statement, whether the ID is 1 or not.

How can I compare these values? I only want the IF-Statement when the ID is 1, else it shall use the ELSE-Statement.

I am retrieving new values via MQTT.

Following my code for my items:

Number Value "Value" <oil> {mqtt="<[mosquitto:/nfc_rfid/xxx/page:state:JSONPATH($.page[4])]"}
String Value_UTC  "Last Update [%s UTC]" <calendar> {mqtt="<[mosquitto:/nfc_rfid/xxx/page:state:JSONPATH($._UTC_timestamp)]"}
String Name "Max " <man_2>
Number ID "PatientID" {mqtt="<[mosquitto:/nfc_rfid/xxx/page:state:JSONPATH($.page[0])]"}

And here my rule:

rule "Value_Changed"
when
	Item Value changed
then
	var ID_C = 1
	if (ID.state == ID_C) {
		sendMail("xxx", "xxx" , "xxx")
		Value.persist
		var min = 35
		var max = 45
		if (Value.state < min){
			sendMail("xxx", "xxx" , "xxx")
			}
		else if (Value.state > max){
			sendMail("x", "x" , "x")
			}
	}else {
		var lastUpdateDate = Value.lastUpdate("mysql").toString()
		Value.postUpdate(Value.previousState.state)
		Value_UTC.postUpdate(lastUpdateDate)
		sendMail("xxx", "Unknown ID" , "xxx")
	}
end

Interestingly, the inner IF-Statement is working just fine, but I don’t know what I am missing/doing wrong comparing the Values in the outer IF-Statement.

Thank you all for your help in advance!
Svain

Numbers are picky things in rules
I would cast your variable as a Number
var Number ID_C = 1
and do the comparison with a Number
if ((ID.state as Number) == ID_C)

That will fail if ID is NULL or UNDEF

These Numbers are really picky…

I tried your suggestion and its giving me…interesting results once again. It sometimes works, sometimes it doesnt and, the most interesting, sometimes it does both the IF AND ELSE-Statement in the same procedure…which makes no sense at all…

Is it worth trying to compare a String instead of a number? Or is there any other “workaround”

Really, it is not. Look for other causes - multiple state updates can cause state changes during rule execution, and as you trigger from ‘changed’ can lead to multiple copies of the rule executing in parallel.

Adding logInfo() to rules during testing is a really useful way to monitor progress and expected values.
e.g. a logInfo("test", "changed triggered " + Value.state.toString)
as the first line will soon show up multiple triggers.

Yeah, I think I know where I trigger the rule twice because I thought about when setting up the Rule at first but never thought about it later again.

When it jumps into the ELSE-Statement I will update the Value with the last persisted Value. So if I understand that correct this should trigger the rule aswell as the Value has changed (once again). But what I dont understand is, where is it getting the old ID from, I am not setting it back to 1 at any given point, therefore it should not trigger the IF-Statement.

I will add LogInfo after each IF/ELSE to clarify how hes cycling through the rule.

To delete my loop I would have to change the trigger for the rule, I will instead check if the ID received an Update (so the rule will trigger every time, not only when the ID changes, which would make no sense in that case IMO cause if the ID stays the same but the value changes, the rule would not trigger?

Thank you very much! Your Suggestion with the logInfo to understand what is happening helped me. It indeed was that I triggered the rule in the ELSE-Statement again which lead to the errors.

Changing it to now listen to an update on the ID instead on a change on the Value helped as I dont manipulate the ID after reading it. Works perfectly fine now, thanks again!