rule "Set Laundry Value"
when
Item virtual_sensor_minute changed
then
if (virtual_sensor_washer_vibrations.state instanceof DecimalType) {
var int y = (virtual_sensor_washer_vibrations.state as DecimalType).intValue
}
else {
var int y = 0}
if (sensor_laundry_washer.state > 220)
{
y = y + 1
postUpdate(virtual_sensor_washer_vibrations,0)
}
else
{
y = y - 1
postUpdate(virtual_sensor_washer_vibrations,0)
}
if (virtual_sensor_washer_vibrations.state > 5)
{
postUpdate(virtual_sensor_washer_switch,ON)
}
else
{
postUpdate(virtual_sensor_washer_switch,OFF)
}
end
Item Number virtual_sensor_washer_vibrations "Vibrations: [%s]"
Error Error during the execution of rule 'Set Laundry Value': An error occured during the script execution: The name 'y' cannot be resolved to an item or type.
Curly brackets denote a context. Anything defined inside curly brackets only exist for that context and goes away outside the context.
To define a variable you start with the keyword var or val.
In the above you have your variable definition for y inside curly brackets which means y ceases to exist when you get to your second set of if else statements.
To fix it, declare y outside the first if else statements.
var int y = 0
if (Yada Yada yada) {
y = (virtual blah blah blah)
}
This concept of scope is pretty universal to most programming languages. Those with a C heritage use curly brackets while other languages use other approaches (e.e. python uses indentation).
I may be wrong, but I think, you have an issue in your rule, because virtual_sensor_washer_vibrations will never count up or down, but stay at 0.
That should be better:
rule "Set Laundry Value"
when
Item virtual_sensor_minute changed
then
if (!(virtual_sensor_washer_vibrations.state instanceof DecimalType))
virtual_sensor_washer_vibrations.postUpdate(0) //if not initialized set to 0
if (sensor_laundry_washer.state > 220)
virtual_sensor_washer_vibrations.postUpdate((virtual_sensor_washer_vibrations.state as DecimalType) + 1)
else
virtual_sensor_washer_vibrations.postUpdate((virtual_sensor_washer_vibrations.state as DecimalType) - 1)
if (virtual_sensor_washer_vibrations.state as DecimalType > 5)
virtual_sensor_washer_switch.postUpdate(ON)
else
virtual_sensor_washer_switch.postUpdate(OFF)
end
Thank you, gentlemen, and apologies for the slow response. 4th of July weekend over here got busy.
In addition to the curly brackets indicating a context, are they treated as one whole statement in an if/then statement? There is no ‘endif’ that I can make work, so I assume when I do
if ()
{}
else
{}
next command
that next command is being executed after the if command regardless of which condition got met.
I’m a pretty good cut/paste scripture between python, bash, javascript and others, but I seem to be having trouble here. Especially when trying to control flow with item states and values. I think I need to go back to basics… need more time as well!
Things that work one moment, fail a few minutes later once I add more code, and then I rewind to where it worked previously, and it still doesn’t, so I reboot and now I’m getting that interpreter error:
2016-07-06 10:09:56.834 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model '._standard.rules' is either empty or cannot be parsed correctly! 2016-07-06 10:10:00.487 [ERROR] [.script.engine.ScriptExecutionThread] - Error during the execution of rule 'ConvertMQTTTemp': Script interpreter couldn't be obtain 2016-07-06 10:10:00.509 [ERROR] [.script.engine.ScriptExecutionThread] - Error during the execution of rule 'Set Washer Status': Script interpreter couldn't be obtain
the ‘set washer status’ rule is not even in standard.rules when this error is thrown.
So frustrating, and I’m wasting so much time when I think something will be simple. Guess that is beta software, though…
Correct. the “}” serves the same purpose as endif in other languages.
But note that if you do not supply the “{” it is invalid to use a “}”. In that case the only statement that is part of the if context is the line immediately following it. Therefore:
if(conditional)
one line of code
is equivalent to
if(conditional) {
one line of code
}
I don’t know OH 2 errors so I can’t be much help there except to suggest this might be caused by a syntax error in your rules file which is causing havoc with the script engine. This could be a mismatched curly bracket (i.e. a { without a matching }) or something like that. You might try getting Designer to load the rule and see if it highlights any syntax errors.
Thanks as always for the awesome responses. You are clearly instrumental in pushing these technologies forward.
I need to play with Designer more, but feel comfortable editing the text and had trouble with Designer early on. Too many challenges - too many things to do.