rlkoshak
(Rich Koshak)
January 17, 2017, 10:50pm
2
I’m going to leave these here:
Major Edit: Rewrote with the cleaner OH 2.3 syntax.
The example lambda on the OH 1 wiki is overly complex and as written will not work in OH 2. This quick example will show a super simple lambda example with copious notes.
Here is the code without comments. It takes in an Item, logs it’s state and returns a slightly modified version of the String logged.
val log= [ GenericItem s |
logInfo("lambda", s.state.toString)
s.state.toString + " logged"
]
rule "Call Lambda"
when
// some t…
Edit: Updates for OH 4, inclusion of JS Scripting and Blockly examples, removed the doors example as it’s superfluous.
Please see Design Pattern: What is a Design Pattern and How Do I Use Them for how to read and use DPs.
Problem Statement
One powerful way to consolidate code in rules is to use array/list manipulation operations on the members of a Group. For example, filter down to just those members that are ON, get a list of the labels of those Items that are NULL, etc.
Concept
[image]
T…
For this second link also look at ben_jones12’s post a little down .
The tl;dr of it is don’t do this. There are much much better ways to reuse the same code for different Items.
But because some people simply must suffer this sort of pain on their own…
I think you can just do:
val physical_relay1 = HardWire_20AMP_Relay
val physical_relay2 = Safety_20AMP_Relay
No quotes.
Furthermore, your rule is incorrect syntax.
if(physical_relay1.state == OFF) {
Kitchen_Heat_Power_STATUS.postUpdate(OFF)
}
else if(pyhsical_relay1.state == ON) {
physical_relay2.sendCommand(OFF)
}
NOTE: do you really mean to check if physical_relay1’s state is ON in the else if? If not then that clause will never be executed. If you really do mean OFF then:
if(physical_relay1.state == OFF){
Kitchen_Heat_Power_STATUS.postUpdate(OFF)
physical_relay2.sendCommand(OFF)
}