Im working through OH2.5 Rules DSL to OH3 updates and I can see there are a number of changes.
This rule keeps track of doors/gates, but errors:
rule "Keep track of the last time a door was opened or closed"
when
Member of gDoorSensors changed
then
if(previousState == NULL) return;
val name = triggeringItemName
val state = triggeringItemName.state.toString
// Update the time stamp
postUpdate(name+"_LastUpdate", now.toString)
// Set the timer if the door is open, cancel if it is closed
if(state == OPEN) sendCommand(name+"_Timer", "ON")
else postUpdate(name+"_Timer", "OFF")
The error:
Script execution of rule with UID 'gategarage-1' failed: 'state' is not a member of 'java.lang.String'; line 9, column 15, length 24 in gategarage
As the name of the variable implies, triggeringItemName is the name of the Item, it’s not the Item itself. A name doesn’t have a state.
But even in OH 2.x you didn’t need to get the state of the triggeringItem directly. What if it changed in the mean time? You should have been using newState to begin with. That’s an implicit variable containing the state that the Item changed to to trigger the rule. So just eliminate the line where you define state and use newState wherever you used state.
No no no. I just don’t know how to say it more clearly.
There’s no newState variable on a String just like there is no state. triggeringItemName is just a String. You can only do String things with it. Strings have no state, no newState, no previousState. You can’t get the members of a String.
It’s just a String, a sequence of letters and numbers. Nothing more.
Look at the if statement I posted. triggeringItemName appears nowhere. You want the state of the Item that triggered the rule with a change? Literally just use newState and just newState. Look at the docs: Rules | openHAB. Those are all the variables that are injected into your rule when it runs and what they are set to.
item.name.toString is redundant. It’s already a String. Just use item.name
triggeringItemName.toString is redundant. It’s already a String
As shown in the docs I linked to, triggeringItem only exists when the rule is triggered by a Member of trigger. This rule is not using a Member of trigger so use newState.toString.
triggeringItemName.newState is the state of that item, THAT WAS A TYPO!
The docs are written by programmers, for programmers. Hence the questions - if it was straight forward I wouldnt ask. This is the crux of the issue, it’s just so complex for non programmers who cant grasp it