I guess part of my issue was my miss-understanding of the Lambda syntax, it was not clear to me that one could/needed to define the Type before the variable, in this case a String
or a OnOffType
… I thought it was a label for the parameter itself, hence I used state
.
I know is always best to show the whole code so help can be more efficient and tailored… I was just trying to get to the point… let’s call it beginner’s experience.
After the example posted with the old syntax, I quickly realized where my problem was and was able to fix it. I was able to define the type as OnOffType as you pointed out (Virtual Studio helped me with this one!).
I should have known better than to know i needed a == to make a test/comparison… I just got frustrated at the lack of progress on my end. My oversight.
I have did read lots of threads and posting, and I was aware that Lambdas carried some level of extra difficulty and constrains, but at least i found it to be an elegant and clean solution… at least in my eyes.
So, for the sake of completeness, I have a Design Pattern rule file to specify different Activities through the day. I have a couple of electrical outlets with an air freshener that I wanted to turn on during Lunch and Dinner time and then turn it off at any other day activity . I have a different rule file with the DP for the Day_Activity definitions and the rule below is a separate rule file on how I execute different commands as the Day Activity changes (I deleted some of the other code lines for other commands, as they are not relevant to the question).
rule "Thrigger Activites based on DayState changes"
when
// Time cron "0/30 0/1 * 1/1 * ? *" or // Run every 30 seconds for debugging purposes
Item Day_Activity changed
then
// Variables / Values: -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
var rule_name = "MyRules.Daystate"
// Functions: -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
val Functions$Function1 <OnOffType,Boolean> outlet_control = [ s1 |
Sycamore_Outlet.allMembers.forEach(item | {
if( item.state != s1 && item.label.toString.contains("Left") ) {
item.sendCommand(s1)
if ( s1 == ON ) { logInfo(rule_name,"We are probably cooking!!! Turning ON the Air Wick on {}.", item.label)}
if ( s1 == OFF ) { logInfo(rule_name,"Turning Off the Air Wick on {}.", item.label)}
}
}) true ]
val Functions$Function1 <OnOffType,Boolean> heater_control = [ s1 |
Sycamore_Heater.allMembers.filter[ i | i.state == ON].forEach(item | {
item.sendCommand(OFF)
logInfo(rule_name,"Forgot to turn off the {}. Turning it OFF ", item.label)
}) true ]
// Main Code: -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
switch Day_Activity.state {
case "WAKEUP": {
//
}
case "GETTING READY": {
heater_control.apply (OFF)
}
case "AT WORK": {
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
outlet_control.apply (OFF)
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
heater_control.apply (OFF)
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Sycamore_Lights.allMembers.forEach(item | {
if( item.state > 0 || item.state == ON) {
item.sendCommand(OFF)
logInfo(rule_name,"Forgot to turn off the {}. Turning it OFF ", item.label)
}
})
}
case "AT HOME": {
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
outlet_control.apply (OFF)
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
heater_control.apply (OFF)
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
}
case "LUNCH": {
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
outlet_control.apply (ON)
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
}
case "DINNER": {
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
outlet_control.apply (ON)
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
}
case "IN BED": {
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
outlet_control.apply (OFF)
}
}
case "SLEEPING": {
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Sycamore_Lights.allMembers.forEach(item | {
if( item.state > 0 || item.state == ON) {
item.sendCommand(OFF)
logInfo(rule_name,"Forgot to turn off the {}. Turning it OFF ", item.label)
}
})
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
outlet_control.apply (OFF)
}
}
}
end
In all, I was able to solve my problem, code the logic as I wanted and I have learned many new things today, and it will help me as I keep learning and using openhab in my house.
Thanks!