Hello folks,
I’m trying to learn openhab design and it’s taking me a lot. A week ago, you helped me to create a Virtual Switch (item) because I didn’t know you can create one. So, I’m trying to build some basic rules with a virtual switch to understand how they work together. Although I am not a programmer, I have a computer science degree so it’s something I can understand and follow the logic.
So I create the following rule. It is as simple as that. It only monitors a Switch and when something “changes” it prints some information in the log. I’m using the variable previousState and it works as expected but with item.state and item.previousState, it’s becoming much more difficult.
Here is the code,
rule "Virtual Switch Change"
when
Item VirtualSwitch changed
then
logInfo("DCG", "Something has changed")
if (previousState == ON) {
logInfo("DCG", "Before ON")
logInfo("DCG", "VS PreviousState " + VirtualSwitch.previousState(false, "mysql").state.toString)
logInfo("DCG", "VS State " + VirtualSwitch.state.toString)
} else {
logInfo("DCG", "Before OFF")
logInfo("DCG", "VS PreviousState " + VirtualSwitch.previousState(false, "mysql").state.toString)
logInfo("DCG", "VS State " + VirtualSwitch.state.toString)
}
end
But the output I’m seeing is this:
2017-12-27 19:49:52.238 [INFO ] [g.eclipse.smarthome.model.script.DCG] - Something has changed
2017-12-27 19:49:52.244 [INFO ] [g.eclipse.smarthome.model.script.DCG] - Before ON
2017-12-27 19:49:52.253 [INFO ] [g.eclipse.smarthome.model.script.DCG] - VS PreviousState OFF
2017-12-27 19:49:52.260 [INFO ] [g.eclipse.smarthome.model.script.DCG] - VS State OFF
2017-12-27 19:50:14.682 [INFO ] [g.eclipse.smarthome.model.script.DCG] - Something has changed
2017-12-27 19:50:14.689 [INFO ] [g.eclipse.smarthome.model.script.DCG] - Before OFF
2017-12-27 19:50:14.699 [INFO ] [g.eclipse.smarthome.model.script.DCG] - VS PreviousState ON
2017-12-27 19:50:14.704 [INFO ] [g.eclipse.smarthome.model.script.DCG] - VS State ON
That doesn’t make sense to me as VirtualSwitch.previousState == VirtualSwitch.state. Before sending this message, I took a look at my persistence service (mysql) and the table looks fine as it has a sequence ON-OFF-ON-OFF what it’s clearly what I’m doing with the switch.
Can someone shed some light about this behaviour? Is it normal?
Thank you very much for your support.
PS: By the way, I’m using the latest openHABian release