[SOLVED]Scene light and persistence rrd4j

  • Platform information:
    • Hardware:RPI3
    • openhab 2.4.0

Hello, I hope to find a solution.
I have set up my entire house, now I would like to go a little further in the persistence. but I can not understand the operation.
What I want to do:
my wife has an office in the dining room.
I want that when she turns on her computer, only some lights (ikea trafri binding) shine stronger, for that I arrive, thanks to the following rule below, and then when she turns off, lights came back to the previous mood light or previous state of light

/*laptop

rule "laptop ON"
when
    Item Laptop changed from OFF to ON 
          if ((Lumieres.state == OFF) && (((hour >= 18)  && (hour <= 23 )) || ((hour >= 4) && (hour <= 8 )))) {
  logInfo("laptop ON", "laptop ON Ambiance bureau")
  sendCommand(Bureau, ON)}
        if (Lumieres.state == ON){
  sendCommand(ArcheDroite, 100)
  sendCommand(ArcheGauche, 100)
  sendCommand(ArcheMilieu, 100)
  sendCommand(Plafond_Salle_A_Manger_3, 100)
  logInfo("laptop ON", "laptop ON Ambiance bureau")        
        }
end

my light moods are manage by rules like this one

rule "bureau"
when
        Item Bureau received command ON
then
      sendCommand(Plafond_Salle_A_Manger_3, 100)
      sendCommand(Lumieres_Arche, 90)

        postUpdate(Lumieres, ON)
        postUpdate(Ambiance_Apero, OFF)
        postUpdate(Ambiance_Diner, OFF)
        postUpdate(Ambiance_Matin, OFF)
        postUpdate(Ambiance_Film_Tele, OFF)
        postUpdate(Ambiance_Tele, OFF)
        postUpdate(Ambiance_Pleine_lumiere, OFF)
        postUpdate(Ambiance_du_Soir, OFF)
        postUpdate(Ambiance_cosy, OFF)
        postUpdate(Ambiance_VP, OFF)
        postUpdate(Ambiance_feutree, OFF)
        postUpdate(Bureau, ON)  
end

, I know, it would be simpler to do like this way :Design Pattern: What is a Design Pattern and How Do I Use Them
but mine is more comprehensive for my mind.

I understood that to recover a previous state I needed a persistence in openhab, so I install rr4dj, (maybe another type would be more appreciated for my case, I’m confused about it), I have configure the following way with the rrd4j.persist file in the persistence folder, this way:

Strategies {
    everyMinute : "0 * * * * ?"
    everyHour 	: "0 0 * * * ?"
    everyDay	: "0 0 0 * * ?"
    default = everyChange
}

Items {
    // persist items on every change and every minute
    * : strategy = everyChange, everyMinute
    Lumieres: strategy = everyChange
}

I also defined the default persistence in openhab
Screenshot_2019-10-09%20Paper%20UI
Some people advise to write something in the cfg file, but i’m not sure, so i didn’t touched it?

and now i’m stuck with my rule:
i don’t know what to do
convert to decimal type ?
restores (item.previous.state)?
when to store my value etc…

i tried this rule: but nothing happens:

rule "laptop OFF"
when
    Item Laptop changed from ON to OFF 
then
var LumieresStates = storeStates(Lumieres_Arche,Lumieres_PLafond_Salle_a_manger)
restoreStates(LumieresStates)
  
  logInfo("laptop OFF", "laptop OFF Ambiance bureau")
  postUpdate(Bureau, OFF)
  
end

i’m aware that this kind of topic is reccurent, i followed multiples topic about persistence, make multiples test, but i’m stuck on something that it sure i don’t understand. I need to understand. thx by advance.

https://www.smarthomeblog.net/openhab-persistence-tutorial/

First things first, please use the method instead of the action:

      sendCommand(Lumieres_Arche, 90)

        postUpdate(Lumieres, ON)

You should do:

      Lumieres_Arche.sendCommand(90)

        Lumieres.postUpdate(ON)

Second, rrd4j only store number values so that would be the wrong choice of persistence

Third, you are almost there:

var LumieresStates //Global variable declared at the top of the rules file

rule "laptop ON"
when
    Item Laptop changed from OFF to ON 
    if ((Lumieres.state == OFF) && (((hour >= 18)  && (hour <= 23 )) || ((hour >= 4) && (hour <= 8 )))) {
        logInfo("laptop ON", "laptop ON Ambiance bureau")
        LumieresStates = storeStates(Bureau)
        Bureau.sendCommand(ON)
    }
    if (Lumieres.state == ON) {
        LumieresStates = storeStates(ArcheDroite, ArcheGauche, Lumieres_PLafond_Salle_A_manger_3)
        ArcheDroite.sendCommand(100)
        ArcheGauche.sendCommand(100)
        ArcheMilieu.sendCommand(100)
        Plafond_Salle_A_Manger_3.sendCommand(100)
        logInfo("laptop ON", "laptop ON Ambiance bureau")        
    }
end

rule "laptop OFF"
when
    Item Laptop changed from ON to OFF 
then
    if (restoreStates !== null) {
        restoreStates(LumieresStates)
        logInfo("laptop OFF", "laptop OFF Ambiance bureau")
        postUpdate(Bureau, OFF)
    }
end

No persistence needed

First, thx you vincent…again…you are everywhere:slight_smile:

first for the method:
i came back to openhab documentation to see the difference and i understand why you said that
second why i don’t need persistence?
third, the rule you wrote return an error:

2019-10-09 17:15:16.405 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'laptop OFF': An error occurred during the script execution: index=0, size=0

just to understand can you confirm?

var LumieresStates 
var Number hour = now.getHourOfDay
/*laptop*/


rule "laptop ON"
when
    Item Laptop changed from OFF to ON or
    Item Boutton received command ON
then
    if ((Lumieres.state == OFF) && (((hour >= 17)  && (hour <= 23 )) || ((hour >= 4) && (hour <= 8 )))) {
        logInfo("laptop ON", "laptop ON Ambiance bureau")
        LumieresStates = storeStates(Bureau)                                                    // store bureau lights scene states when item ON
        Bureau.sendCommand(ON)
    }
    if (Lumieres.state == ON) {
        LumieresStates = storeStates(ArcheDroite, ArcheGauche, Plafond_Salle_A_Manger_3)        // store global lights states
        ArcheDroite.sendCommand(100)
        ArcheGauche.sendCommand(100)
        ArcheMilieu.sendCommand(100)
        Plafond_Salle_A_Manger_3.sendCommand(100)
        logInfo("laptop ON", "laptop ON Ambiance bureau")        
    }
end

rule "laptop OFF"
when
    Item Laptop changed from ON to OFF or
    Item Boutton received command OFF
then
    if (restoreStates !== null) {                                   // can u explain this line ?
        restoreStates(LumieresStates)                               // restore light state to previous one
        logInfo("laptop OFF", "laptop OFF Ambiance bureau")
        postUpdate(Bureau, OFF)
    }
end

Apologies:

rule "laptop OFF"
when
    Item Laptop changed from ON to OFF or
    Item Boutton received command OFF
then
    if (LumiereStates !== null) {                                   // Does it make sense now?
        restoreStates(LumieresStates)                               // restore light state to previous one
        logInfo("laptop OFF", "laptop OFF Ambiance bureau")
        postUpdate(Bureau, OFF)
    }
end

You don’t need persistence because storeStates and restoreStates don’t need persistence
You are storing the states in a variable not a database

Offtopic question:
Are the methods “storestates” and " restoresrates" part of the standard textual rules?
IMHO they 're only mentioned for the JSR223, which needs the NGRE.

okay i make some tests to understand and now it’s clear to me, i’ve change the rule in order to switch all the lights off when lumieres.state ==OFF before triggering “bureau”

rule "laptop ON"
when
    Item Laptop changed from OFF to ON or
    Item Boutton received command ON
then
    if ((Lumieres.state == OFF) && (((hour >= 17)  && (hour <= 23 )) || ((hour >= 4) && (hour <= 8 )))) {
        logInfo("laptop ON", "laptop ON Ambiance bureau")
        LumieresStates = storeStates(Lumieres)                                     // <===  HERE                                                
        Bureau.sendCommand(ON)
    }
    if (Lumieres.state == ON) {
        LumieresStates = storeStates(ArcheDroite, ArcheGauche, Plafond_Salle_A_Manger_3)        
        ArcheDroite.sendCommand(100)
        ArcheGauche.sendCommand(100)
        ArcheMilieu.sendCommand(100)
        Plafond_Salle_A_Manger_3.sendCommand(100)
        logInfo("laptop ON", "laptop ON Ambiance bureau")        
    }
end

rule "laptop OFF"
when
    Item Laptop changed from ON to OFF or
    Item Boutton received command OFF
then
    if (LumieresStates !== null) {                                   
        restoreStates(LumieresStates)                               
        logInfo("laptop OFF", "laptop OFF Ambiance bureau1")
        postUpdate(Bureau, OFF)
    }
end

and thx to you i really understand the variables role !!! thank you again vincent

They are part of the DSL and are actions not methods
There is documentation… somewhere…

1 Like