How to make openhab to remember GPIO states before reset?
And how to make openhab to set remembered states after restart.
I have persistence rrd4j and i have rule to persist every change i have rule to roll back all persisted info after reset, bit not working good, every time after reset give me exact same GPIO output states no meter how is set before reset.
restoreOnStartup will only restore the state of the Item within openHAB. It does not command the GPIO to change its state when OH restarts. You will have to write a rule for that:
Create an Unbound Item that stores the most recent value of the Item the GPIO is bound to
Create a rule to update this Unbound Item whenever the GPIO bound Item updates
Make sure the Unbound Item is restored on startup
Create a System started rule that commands the GPIO bound Item with the restored state of the Unbound Item.
An unbound item is one that has not binding. I.e. no { }.
rule "Update unbound Item"
when
Item MyGPIO changed
then
MyUnboundGPIO.postUpdate(MyGPIO.state)
end
rule "System started"
when
System started
then
MyGPIO.sendCommand(MyUnboundGPIO.state)
end
Is there any reason why isn’t this working in OH2.1?
I’m getting type mismatch when using .state as parameter in sendCommand.
However I got it working in a little more complicated way by using if statement I would prefer the one line solution
My workaround:
rule “System started”
when
System started
then //RedLED.sendCommand(RedLED_ub.state)
if (RedLED_ub.state==ON){
RedLED.sendCommand(ON)
}
if (RedLED_ub.state==OFF){
RedLED.sendCommand(OFF)
}
end
rule “System started”
when
System started
then
RedLED.sendCommand(RedLED_ub.state)
A0.sendCommand(A0_ub.state)
B7.sendCommand(B7_ub.state)
//if (RedLED_ub.state==ON){
// RedLED.sendCommand(ON)
//}
//if (RedLED_ub.state==OFF){
// RedLED.sendCommand(OFF)
//}
end
Log:
18:41:40.321 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule ‘System started’: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,org.eclipse.smarthome.core.types.Command) on instance: null
18:41:40.326 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘RedLED’ received command ON
Since this is a System started Rule, I’m going to guess that what is happening is the Item is not fully initialized when the Rule runs. You are not seeing errors now because the rule only does something if the item has been initialized. I bet if you added an else with a log you will find that the rule isn’t doing anything some it all of the time.
You can add a Thread::sleep to the Rule to give everything a chance to become initialized.
It look like the mcp23017 is initialized and your solution is working.
The problem was that I had a sitemap which inverted the status and this lead to incorrect rule action.
Switch item=A0 mappings=[OFF=ON, ON=OFF]
It is not related to this topic anymore but I had this in my sitemap because the MCP23017 addon is sending a logic high when I need a low and vice versa so for example my relays are switching on when an OFF command is sent.
Inversing the logic in the sitemap might not be the best idea.
I need to find out something…maybe will do it with unbound items.