Rule is not working anymore

Hi,

I used a script base rule that checked if my washing machine has finished it´s job. OH3 imported it successfully and splitted it into 3 parts (don´t know why, but it worked.)
Unfortunately the rule does not work since I updated the system the ver 3.4.0. I don´t know why, it just seems to not trigger.
So I tried to recreate the rule in the Rule editor like this:

Name: Wasching machine ready message

When Item washingmachine is updated
execute given script. The script:

val Number MODE_OFF = 0
val Number MODE_STANDBY = 1
val Number MODE_ACTIVE = 2
val Number MODE_FINISHED = 3
var Boolean cycle = 0

rule "Waschmaschine fertig"
when
    Item Waschmaschine_WaschmaschineVerbrauchAktuell changed
then
 if (Waschmaschine_WaschmaschineVerbrauchAktuell.state > 100 )
		{
		Waschmaschine_OpState.postUpdate(MODE_ACTIVE)
		cycle=0
		}
		else if (Waschmaschine_WaschmaschineVerbrauchAktuell.state <= 5 && Waschmaschine_OpState.state == MODE_ACTIVE)
		{		
		Waschmaschine_OpState.postUpdate(MODE_FINISHED)
		}
		
end

Next corresponding rule:

Name: Washingmachine send notification

When Item OpState has changed
execute given script. The script:

val Number MODE_OFF = 0
val Number MODE_STANDBY = 1
val Number MODE_ACTIVE = 2
val Number MODE_FINISHED = 3


rule "Waschmaschine Nachrichten"
when
    Item Waschmaschine_OpState changed
then
    if (Waschmaschine_OpState.state == MODE_FINISHED) {
        sendNotification("XXX@web.de", "Waschmaschine fertig!") //Nachricht wird hiermit abgesendet
		EchoWzTextToSpeech_Sprich.sendCommand('Waschmaschine fertig')
    }
end

Any idea why it´s not working anymore?

edit: I found out the the item Waschmaschine_OpState hasn´t changed for a few weeks now. Did sth. change in the “postUpdate” method?

BTW This is what I´ve created using ChatGPT, should it run?

rule "Waschmaschine Verbrauch Überwachung"
when
  Item Waschmaschine_WaschmaschineVerbrauchAktuell changed
then
  if (Waschmaschine_WaschmaschineVerbrauchAktuell.state > 100) {
    Waschmaschine_OpState.postUpdate(1)
  } else if (Waschmaschine_WaschmaschineVerbrauchAktuell.state < 6 && Waschmaschine_OpState.state == 1) {
    Waschmaschine_OpState.postUpdate(2)
  } else if (Waschmaschine_WaschmaschineVerbrauchAktuell.state < 6 && Waschmaschine_OpState.state == 2) {
    Waschmaschine_OpState.postUpdate(0)
    sendNotification("XXX@web.de", "Waschmaschine fertig!")
  }
end

edit2: To make it easier I created a new rule in the OH3 UI but this time as a script. There I used the ChatGPT code above.
After injecting some states (using nodeRed) I can verify from logviewer that the states change, but no rule is ever triggered. What´s going on here??

edit3: Just had a look in my openhab.log, wow, what srcewed my rules liket this??:

2023-02-06 15:00:55.483 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'waschgeraete-1' failed: The name 'sendNotification' cannot be resolved to an item or type; line 14, column 9, length 59 in waschgeraete
2023-02-06 15:00:57.059 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '34a4c0adb7' failed: var Integer Notification1_Sent = 0
var Integer Notification2_Sent = 0
val Number MODE_OFF = 0
val Number MODE_STANDBY = 1
val Number MODE_ACTIVE = 2
val Number MODE_FINISHED = 3


rule "Waschmaschine Nachrichten"
when
    Item Waschmaschine_OpState changed
then
    if (Waschmaschine_OpState.state == MODE_FINISHED) {
        sendNotification("XXX@web.de", "Waschmaschine fertig!") //Nachricht wird hiermit abgesendet
		EchoWzTextToSpeech_Sprich.sendCommand('Waschmaschine fertig')
    }
end

   1. The method or field rule is undefined; line 9, column 180, length 4
   2. The method or field when is undefined; line 10, column 213, length 4
   3. The method or field changed is undefined; line 11, column 249, length 7
   4. The method or field then is undefined; line 12, column 257, length 4
   5. The method or field end is undefined; line 17, column 492, length 3
   6. This expression is not allowed in this context, since it doesn't cause any side effects.; line 9, column 185, length 27
   7. This expression is not allowed in this context, since it doesn't cause any side effects.; line 11, column 222, length 4
   8. This expression is not allowed in this context, since it doesn't cause any side effects.; line 11, column 227, length 21

You mean in the UI as a UI rule?

That’s never going to work like that. To start review Rules - Introduction | openHAB and Rules | openHAB.

You’ve tried to put an entire .rules file’s contents into a Script Action. That’s never going to work. The triggers need to be defined in the triggers section of the rule. The contents of the Script Action needs to be only the stuff between the then and end.

You can move those constants to be inside the Script Action too, or if more than one rule needs them move them to a Items.

No, nothing changed with postUpdate. But be aware that your rules as written trigger on that Item changing state. So that’s the root cause of your problem. The original rules that worked probably still work.

This may be the first example of a rule created by ChatGPT that is at least syntactically correct and logically reasonable (meaning unless you can write the rule yourself ChatGPT is not going to be a useful tool because it almost always gets it wrong). But again, this will only work in a .rules file like this. In a UI rule you define the trigger in the Triggers section separately and only the stuff between the then and end goes into a Script Action.

However, note that there could be cases where this rule never does anything. For example, if Waschmaschine_WaschmaschineVerbrauchAktuell.state remains at or above 6, the rule will never do anything.




4

Pls have a look at my new Rule set up, this should work, right!?

edit: I also tried using Waschmaschine_OpState as a string, but still no success. Waschmaschine_OpState is not updated.

edit2: not even this is working:

Once the rule is saved I assume it shows a state “IDLE” and no error state.

Is the triggering Item changing state? You can see that in events.log.

Is the state of that Item either greater than 100 or less than 6? If not the rule won’t do anything. If it’s less than 6, is Waschmachine_OpState 1 or 2? If not than the rule won’t do anything.

I´m using nodeRed to inject item states. I update Waschmaschine_WaschmaschineVerbrauchAktuell to 160 manually. I can see this update in the logviewer. The rule does not trigger.

Yes, it´s IDLE, no error state

events.log:

2023-02-06 19:38:41.655 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Waschmaschine_WaschmaschineVerbrauchAktuell' changed from 0 W to 160 W
2023-02-06 19:38:56.043 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Waschmaschine_WaschmaschineVerbrauchAktuell' changed from 160 W to 0 W

What happens when you run the rule manually?

Add logging so it logs out something no matter what to see whether the problem is the rule isn’t running or there is something logically wrong.

Isn’t a unit required to be used in the comparison as the item state has a value and a unit ?

1 Like

Good point, but that should generate an error, right? It’s been a long time since I’ve used Rule DSL.

how do I add logging? The Event Monitor gives just this output when I manually prees play on the rule:

* openhab/rules/1294afb204/state

RuleStatusInfoEvent

{"status":"IDLE","statusDetail":"NONE"}

edit: nothing of this rule is logged in openhab.log


Wow, I finally figured it out…

Here´s my working rule (I also changed my OpState-states from numbers to textual Information):

if (Waschmaschine_WaschmaschineVerbrauchAktuell.state > 100) {
    Waschmaschine_OpState.postUpdate("Active")
  } else if (Waschmaschine_WaschmaschineVerbrauchAktuell.state < 6.0 && Waschmaschine_OpState.state == "Active") {
    Waschmaschine_OpState.postUpdate("Standby")
  } else if (Waschmaschine_WaschmaschineVerbrauchAktuell.state < 6.0 && Waschmaschine_OpState.state == "Standby") {
    Waschmaschine_OpState.postUpdate("Finished")
    sendNotification("XXX@web.de", "Waschmaschine fertig!")
    EchoWzTextToSpeech_Sprich.sendCommand("Waschmaschine fertig!")  
  }

The problem was the configuration of my items, especially the Waschmaschine_WaschmachineVerbrauchAktuell item: it was set to
“Type: Number:Power”. So probably what @Wolfgang_S mentioned. So I reconfigured it to “Type: Number” et voila, it works. My item “WaschmaschineOpState” is and was a String.

So it works now, but: I would like to keep my “Waschmaschine_WaschmaschineVerbrauchAktuell” as an “Number:Power” -item but I don´t know what I have to set the Values in my rule, e.g.
if (Waschmaschine_WaschmaschineVerbrauchAktuell.state > 100 W)
did not work. Any idea how to fix that, too?

Anyway, it´s strange it worked before my update to OH 3.4.0.

That’s because the rule doesn’t log anything. See Actions | openHAB

Thank you, I edited my post above your last post…

It’s always a good idea to consult the docs for answers to questions like these. Textual Rules | openHAB

if (Waschmaschine_WaschmaschineVerbrauchAktuell.state > |100 W)

should work. If it complains either of these should work

if (| 100W < Waschmaschine_WaschmaschineVerbrauchAktuell.state)

if ((Waschmaschine_WaschmaschineVerbrauchAktuell.state as QuantityType<?>) > | 100 W)
1 Like

That´s it, I´m absolutely happy now!

Thank you very much!