How to make openhab to remember GPIO states befor reset?

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.

See https://github.com/openhab/openhab/wiki/rrd4j-Persistence

NOTE: rrd4j is for storing numbers only. Attempting to use rrd4j to store complex datatypes (eg. for restore-on-startup) will not work.

You have to use another persistence to store other values

@rlkoshak

What is bound and unbound?

restoreOnStartup only restore state of items. But this items control GPIO is the same at the end the result is changing state on GPIO.

i haven’t any idea how have to look like this kind of rules.

Starting to think other option to connect battery power arduino between GPIO and relay board storing last value in eeprom

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

Hi!

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 :slight_smile:

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

Thanks!

It should work. Please post the error in the logs for the not working line.

Well, it looks like you are right in spite of the mismatch error it is working for GPIO items but is not working for some other items.

A0 and B7 items are:

Switch A0 “Test A0” { mcp23017="{ address:20, pin:‘A0’, mode:‘DIGITAL_OUTPUT’, defaultState:‘HIGH’}"}
Switch B7 “Test B7” { mcp23017="{ address:20, pin:‘B7’, mode:‘DIGITAL_OUTPUT’, defaultState:‘HIGH’}"}

Rules:

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.

If the binding allows transforms, you can use a MAP or JS transform to swap the states coming from the binding to what you need.