Writing Item Value to variable

Hi,
I need a little bit help writing a rule for switch off heating and turn it an again if window is closed or open.
My try dosn’t work:

/**CONTACT */
rule "BAD EG Fenster offen"
when Item EG_B_CONTACT changed
then
	val EG_B_TEMP = EG_B_ST.value as DecimalType
	if(EG_B_CONTACT.state == OPEN) {
		sendCommand(EG_B_Heat_SW, OFF)
	}
	if(EG_B_CONTACT.state == CLOSED) {
		sendCommand(EG_B_ST, EG_B_TEMP)
	}
end

My Idea is to set the heater to the last value before window was opened. Can anyone give me a hint?
EG_B_ST is an Homematic automatic valve ST is “SetTemperature”

Thanks a lot Christoph

Please review

http://docs.openhab.org/configuration/rules-dsl.html

When you have a rule that doesn’t work please:

  1. Load the rules in Designer to check for syntax errors

  2. Post any relevant logs, in particular errors in the logs.

In this case I suspect you are seeing an error saying it can’t parse your .rules file because it has no idea what the heck EG_B_ST.value is. To get the “value” of an Item you use .state.

From a logical perspective the rule doesn’t make sense either. EG_B_TEMP gets set to the current state of EG_B_ST. then if the EG_B_CONTACT is CLOSED your send this same number right back to EG_B_ST. You never actually save the previous value anywhere.

There are two ways you can do this.

First you can move EG_B_TEMP to be a global and populate it when the contact state is OPEN:

var EG_B_TEMP = -1

rule "BAD EG Fenster offen"
when 
    Item EG_B_CONTACT changed
then
	if(EG_B_CONTACT.state == OPEN) {
    	EG_B_TEMP = EG_B_ST.value as Number
		EG_B_Heat.sendCommand(OFF)
	}
	if(EG_B_CONTACT.state == CLOSED) {
		EG_B_ST.sendCommand(EG_B_TEMP)
	}
end

Note: I made some additional changes that makes the code more reliable.

The second way involves persistence which would result in shorter code but I don’t think it will handle the case where someone changes the target temp while the window is open very well.

1 Like

Hi Rich, thanks for your reply. I will try to adapt the rule the next few days.
What I missed or not found is a kind of reference or documentation for programming rules.
Http://docs.openhab.org/configuration/rules-dsl.html here are only a few examples listed.

Christoph

That link has links to further documentation including to the Xtend language upon which the Rules DSL is based.

Solved this issue with persistence.
Here is my solution:

/** BAD EG */                                                                                                                                                                                                      
rule "Heizung Bad EG"                                                                                                                                                                                              
when Item EG_B_Heat_SW changed                                                                                                                                                                                     
then                                                                                                                                                                                                               
        if(EG_B_Heat_SW.state == ON && EG_B_ST.state == 4.5) {                                                                                                                                                     
                EG_B_ST.sendCommand(EG_B_ST.previousState.state as Number)                                                                                                                                         
                logInfo("Testing", "Heizung Bad: AN")                                                                                                                                                              
        }                                                                                                                                                                                                          
        if(EG_B_Heat_SW.state == OFF) {                                                                                                                                                                            
                EG_B_ST.persist                                                                                                                                                                                    
                EG_B_ST.sendCommand("4.5")                                                                                                                                                                         
                logInfo("Testing", "Heizung Bad: AUS")                                                                                                                                                             
        }                                                                                                                                                                                                          
end                                                                                                                                                                                                                
                                                                                                                                                                                                                   
rule "set B_Heat_SW EG"                                                                                                                                                                                            
when Item EG_B_ST changed from 4.5                                                                                                                                                                                 
then                                                                                                                                                                                                               
        EG_B_Heat_SW.sendCommand(ON)                                                                                                                                                                               
end                                                                                                                                                                                                                
                                                                                                                                                                                                                   
/**CONTACT */                                                                                                                                                                                                      
rule "BAD EG Fenster offen"                                                                                                                                                                                        
when Item EG_B_CONTACT changed                                                                                                                                                                                     
then                                                                                                                                                                                                               
        if(EG_B_CONTACT.state == OPEN) {                                                                                                                                                                           
                logInfo("Testing", "Aktueller Status Heizung: " + EG_B_Heat_SW.state)                                                                                                                              
                logInfo("Testing", "Aktueller Wert: " + EG_B_ST.state)                                                                                                                                             
                EG_B_ST.persist                                                                                                                                                                                    
                EG_B_Heat_SW.persist                                                                                                                                                                               
                EG_B_Heat_SW.sendCommand(OFF)                                                                                                                                                                      
        }                                                                                                                                                                                                          
        if(EG_B_CONTACT.state == CLOSED) {                                                                                                                                                                         
                logInfo("Testing", "Letzter Status Heizung: " + EG_B_Heat_SW.previousState.state)                                                                                                                  
                logInfo("Testing", "Letzter Wert: " + EG_B_ST.previousState.state as Number)                                                                                                                       
                EG_B_Heat_SW.sendCommand(EG_B_Heat_SW.previousState.state.toString )                                                                                                                               
                EG_B_ST.sendCommand(EG_B_ST.previousState.state as Number)                                                                                                                                         
        }                                                                                                                                                                                                          
end                                                                                                                                                                                                                
/** Ende BAD EG */