[SOLVED] Mapdb not reinstating item properly

rules
mapdb
openhab2
Tags: #<Tag:0x00007f0144a04820> #<Tag:0x00007f0144a046e0> #<Tag:0x00007f0144a04528>

(Nikhil Komalan) #1

Hello,

I am currently using openhab2 on RPi3.

I have installed mapdb persistence and looking to restore my last value form DB. I have succesfully restored values for relay, dc motor etc that means my MapDB setup is working fine.

I am trying to control a Stepper motor now. And below i have given my configuration files for the same.

Here goes my demo.items file:

String EPBM4 "EPBM4 [%s]" { serial="/dev/ttyUSB0" }
Switch st_start "Start/Stop" <garagedoor>
Switch st_clk "Clockwise/Anti-Clockwise" 
Number st_steps "Steps in Degree [%d deg]" 

Here goes my Demo.rules file:

var String st_clkFlag
var Number st_stepsFlag


rule "ST_Step"
when   
  Item st_steps received command  
then  
  var Number st_percent = st_steps.state as DecimalType 

 st_stepsFlag = st_percent
   postUpdate(st_steps, st_stepsFlag)
end 


rule "ST_Clk"
when   
 Item st_clk changed  
then  
  var state = st_clk.state as OnOffType  
  if(state == ON){
  	st_clkFlag = 'C';
  }
  if(state == OFF){
  	st_clkFlag = 'A';
  }
end 


rule "ST_Start"
when   
 Item st_start changed
then
 var state = st_start.state as OnOffType
 if(state == ON){
 	sendCommand (EPBM4 , "S" + "ON" + st_clkFlag + st_stepsFlag + "\r")
 }
 if(state == OFF){
 	sendCommand (EPBM4 , "SOFF" + "\r")
 }
end

Here is my sitemap:

sitemap demo label="Main Menu"
{
Frame label="EPBM4 Serial" icon="socket" {
				Text item=EPBM4								
				Switch item=st_start					
				Switch item=st_clk					
				Setpoint item=st_steps minValue=0 maxValue=100 step=5								
		}			
	}		

My mapDB config file are given below for reference:

mapdb.persist

Strategies {
default = everyUpdate
 }

Items {
*: strategy = everyChange, restoreOnStartup 

}

mapdb.cfg (haavent made any change)

     # the commit interval in seconds (optional, default to '5')
    #commitinterval=5

# issue a commit even if the state did not change (optional, defaults to 'false')
#commitsamestate=false

Error:
*My item “EPBM4” is stored correctly in Database,

   {"name":"EPBM4","datapoints":"1","data":[{"time":1543564644523,"state":"SONC100\r"}]}

But when i restart openhab, the item values get updated succesfully but Motor dont rotates as the EPBM4 is getting Null value in string.

Here is event.log file for better understanding:

13:29:33.662 [DEBUG] [org.openhab.persistence.mapdb ] - ServiceEvent REGISTERED - {org.openhab.core.persistence.PersistenceService}={service.id=379, service.bundleid=242, service.scope=bundle, component.name=org.openhab.persistence.mapdb, component.id=248} - org.openhab.persistence.mapdb
13:29:33.696 [INFO ] [smarthome.event.ItemStateChangedEvent] - EPBM4 changed from NULL to SONC100
13:29:33.701 [INFO ] [smarthome.event.ItemStateChangedEvent] - st_start changed from NULL to ON
13:29:33.706 [INFO ] [smarthome.event.ItemStateChangedEvent] - st_clk changed from NULL to ON
13:29:33.711 [INFO ] [smarthome.event.ItemStateChangedEvent] - st_steps changed from NULL to 100
13:29:33.730 [DEBUG] [org.openhab.persistence.mapdb ] - BundleEvent STARTED - org.openhab.persistence.mapdb
13:29:35.908 [INFO ] [smarthome.event.ItemCommandEvent ] - Item ‘EPBM4’ received command SONCnull
13:29:35.926 [DEBUG] [apdb.internal.MapDBPersistenceService] - store called for EPBM4
13:29:35.942 [INFO ] [smarthome.event.ItemStateChangedEvent] - EPBM4 changed f to SONCnull

Can anyone help me, why this null value is arising as last value stored in DB was correct.


(Angelos) #2

it’s not null … it’s SONCnull
since you are composing this from:

sendCommand (EPBM4 , "S" + "ON" + st_clkFlag + st_stepsFlag + "\r")

check why the value of your var Number st_stepsFlag is null

It may be due to the following situation:

  1. Map restores the state of EPBM4 to SONC100\r but then the rule fires and updates it with SONCnull
    or
  2. Map has stored SONCnull as the last value

(Nikhil Komalan) #3

Map stores the values correctly. Might be problem with rule.


(Nikhil Komalan) #4

Okay my issue was with the above rule.
Here is the code thats working,

 rule "ST_Step"
 when   
    Item st_steps changed
then  
    var Number st_percent = st_steps.state as DecimalType 

    st_stepsFlag = st_percent
     postUpdate(st_steps, st_stepsFlag)
   end 

Thankyou :slight_smile: