Putting variable instead of item.state in openhab rules and sendCommand problems

Hi,

I’ve built a scheduler using some of the tutorials here and i have some problem.

Items

Switch HVAC_VALVE01_SCHED05_ONOFF "Scheduler ONOFF"
Number HVAC_VALVE01_SCHED05_URA "Hour"
Number HVAC_VALVE01_SCHED05_MINUTA "Minute"
Switch HVAC_VALVE01_SCHED05_PO "Monday"
Switch HVAC_VALVE01_SCHED05_TO "Tuesday"
Switch HVAC_VALVE01_SCHED05_SR "..."
Switch HVAC_VALVE01_SCHED05_CE "..."
Switch HVAC_VALVE01_SCHED05_PE "...."
Switch HVAC_VALVE01_SCHED05_SO "..."
Switch HVAC_VALVE01_SCHED05_NE "..."
Switch HVAC_VALVE01_SCHED05_VALVE_SET "Set item t this value after scheduler runs"

Rule

rule "Urnik Openhab v2.2"
when
	Time cron "0 0/1 * * * ?"   // every minute
then
	var Dan = ""
    
	// Get day of a week and write it to variable that has item name
    switch now.getDayOfWeek{
        case 1: Dan = "HVAC_VALVE01_SCHED01_PO"
        case 2: Dan = "HVAC_VALVE01_SCHED01_TO"
        case 3: Dan = "HVAC_VALVE01_SCHED01_SR"
        case 4: Dan = "HVAC_VALVE01_SCHED01_CE"
        case 5: Dan = "HVAC_VALVE01_SCHED01_PE"
        case 6: Dan = "HVAC_VALVE01_SCHED01_SO"
        case 7: Dan = "HVAC_VALVE01_SCHED01_NE"
    }

	logInfo("DEBUG", "DEBUG 1")

	// Check if scheduler is enabled
    if(HVAC_VALVE01_SCHED01_ONOFF.state == ON) {
		logInfo("DEBUG", "DEBUG 2")
    	logInfo("DEBUG", "Urnik je vklopljen")
		// Check if this day is enabled
    	if(Dan.state == ON) {
    		logInfo("DEBUG", "Danasnji dan je vklopljen")
			// Check time
    		if(HVAC_VALVE01_SCHED01_MINUTA.state == now.getMinuteOfHour && HVAC_VALVE01_SCHED01_URA.state == now.getHourOfDay) {
				// Run scheduler
				HVAC_VALVE_01_STS.sendCommand(HVAC_VALVE01_SCHED01_VALVE_SET.state as StringType)
    		} else {
    			logInfo("DEBUG", "Urnik NI bil izveden")
    		}
    	}
    }
end

My problem is that i cannot use Dan.state so i can run code if day matches. If i set it to fixed item name it works.
I also want to write HVAC_VALVE01_SCHED01_VALVE_SET item value to item HVAC_VALVE_01_STS but it does not work like this.

Cand anyone help me with that?

In your code, Dan is declared a variable … i.e

var Dan = ""

Dan becomes a variable of type String because you assign “” to it when it is declared. State is not a member of the object type String, hence the code if(Dan.state == ON) will never work.

The way I do it (and I’m not sure this is the most efficient way) is to put the items in a group.

Group gSwitches
Switch HVAC_VALVE01_SCHED05_PO "Monday" (gSwitches)
Switch HVAC_VALVE01_SCHED05_TO "Tuesday" (gSwitches)
Switch HVAC_VALVE01_SCHED05_SR "..." (gSwitches)
Switch HVAC_VALVE01_SCHED05_CE "..." (gSwitches)
Switch HVAC_VALVE01_SCHED05_PE "...." (gSwitches)
Switch HVAC_VALVE01_SCHED05_SO "..." (gSwitches)
Switch HVAC_VALVE01_SCHED05_NE "..." (gSwitches)

Then do this.

var SwitchItem i =  gSwitches.members.findFirst[name.equals(Dan)] as SwitchItem
if (i !== null) {
    if (i.state == ON) {
        // Do stuff
    }
}