[SOLVED] Lambda not being called

Hi there, dear OH-enthusiasts,

I use the following rule to configure my appartment’s heating for the night. The first argument to sendCommandOnChange is an item, the second a desired temperature stored in an item.

rule "night"
when
 Time cron "00 45 21 * * ?"
then
 logInfo("heatingAutomation", "night")
 
 sendCommandOnChange(HeizungSchlafenSoll,   (HeizungSchlafenVollTemp.state as DecimalType).intValue)
 sendCommandOnChange(HeizungWohnzimmerSoll, (HeizungWohnzimmerLeerTemp.state as DecimalType).intValue)
 sendCommandOnChange(HeizungKuecheSoll,     (HeizungKuecheLeerTemp.state as DecimalType).intValue)
 sendCommandOnChange(HeizungBadSoll,        (HeizungBadLeerTemp.state as DecimalType).intValue)
end

In order to save precious resources, my goal is to actually send temperature change commands only if there is a change from the current desired temperature (heaters are controlled by some quite sluggish radio controlled proprietary hardware). I’d like to accomplish this using the following lambda (which comes before the actual rule in the heatingAutomation.rules-file).

val sendCommandOnChange = [ GenericItem itm, Integer intVal |
 if (intVal != (itm.state as DecimalType).intValue) {
  itm.sendCommand(intVal)
 }
]

However, I get the following error in the logs: Validation issues found in configuration model ‘heatingAutomation.rules’, using it anyway: This expression is not allowed in this context, since it doesn’t cause any side effects. From what I understood this can be fixed by returning some arbitrary value from the lambda (put e.g. true in it’s last line) and assigning the return value (to some dummy variable).

However, even then the lambda does not seem to be called, i.e. additional logInfo(…) commands in the lambda do not occur in the log. What am I doing wrong?!

Thanks a bunch for your kind help, guys!
ugh_bough

  • Platform information:
    • Hardware: RaspberryPi
    • OS: Openhabian
    • openHAB version: 2.3.0

Try that:

val Procedures$Procedure2<GenericItem, int> sendCommandOnChange = [ itm, intVal |
    if (intVal != (itm.state as DecimalType).intValue) {
        itm.sendCommand(intVal)
    }
]
1 Like

Thanks! No validation errors. However, still nothing in the logs… If someone has more ideas, let me know. Meanwhile, I’ll keep trying :slight_smile:

Cheerio!

You call it with myLambdaName.apply(param1,param2)

1 Like

Yes, of course… Duh!!

Just returned to report the same, but you were quicker! :slight_smile: Thanks, guys!

Full working example:

val sendCommandOnChange = [ GenericItem itm, Integer intVal |
 logInfo("heatingAutomation", itm.name + " " + itm.state + "->" + intVal)
 if (intVal != (itm.state as DecimalType).intValue) {
  itm.sendCommand(intVal)
 }
] 
 
rule "night"
when
 Time cron "00 35 12 * * ?"
then
 logInfo("heatingAutomation", "night")
 
 sendCommandOnChange.apply(HeizungSchlafenSoll, (HeizungSchlafenVollTemp.state as DecimalType).intValue)
 sendCommandOnChange.apply(HeizungLivingRoomSoll, (HeizungLivingRoomLeerTemp.state as DecimalType).intValue)
 sendCommandOnChange.apply(HeizungKuecheSoll, (HeizungKuecheLeerTemp.state as DecimalType).intValue)
 sendCommandOnChange.apply(HeizungBadSoll, (HeizungBadLeerTemp.state as DecimalType).intValue)
end

Cool, please mark the thread as solved, thanks