Reusable Functions: A simple lambda example with copious notes

I’ve only ever used Python though the Helper Libraries which I think are not yet OH 3 compatible. But you can look at that code to see how to create a rule without using the Helper Library. When using the Helper Library a rule would look something like:

from core.rules import rule
from core.triggers import when
from core.utils import sendCommandCheckFirst
from configuration import weather_icon_path
import subprocess
from javax.imageio import ImageIO
from java.io import File

@rule("Is Cloudy", description="Generates an event when it's cloudy or not",
      tags=["weather"])
@when("Item vCloudiness changed")
def is_cloudy(event):
    """Sets a switch to ON when cloudiness gets above 50%."""

    newState = "ON" if items["vCloudiness"] > QuantityType(u"50.0 %") else "OFF"
    sendCommandCheckFirst("vIsCloudy", newState)

@rule("Weather Icon",
      description="Copy the current weather conditions icon",
      tags=["weather"])
@when("Item vWeather_Conditions_Icon changed")
@when("System started")
def cond_icon(event):
    """
    Download the weather conditions icon and convert it from gif to png.
    """

    cond_icon.log.info("Fetching the weather conditions icon... {}"
                       .format(ir.getItem("vWeather_Conditions_Icon").state))
    dl = subprocess.Popen(['/usr/bin/wget', '-qO-',
                  'http://argus:8080/rest/items/vWeather_Conditions_Icon/state'],
                  stdout=subprocess.PIPE)
    dd = subprocess.Popen(['/bin/dd', 'bs=22', 'skip=1'], stdin=dl.stdout,
                          stdout=subprocess.PIPE)
    dl.wait()
    f = open(weather_icon_path, "w")
    subprocess.call(['/usr/bin/base64', '-d'], stdout=f, stdin=dd.stdout)
    dd.wait()

This file is located in /etc/openhab/automation/jsr223/python/personal.

I don’t use JavaScript outside of UI created rules but I do have JavaScript libraries. You can find a discussion on how to do it in JavaScript at OH 3 Examples: Writing and using JavaScript Libraries in MainUI created Rules. I particularly point this post out because your lambda could be a library function that gets called.

Even way back in the OH 2.0 time frame I’ve been telling users who have even a little bit of experience with programming to not use Rules DSL. Rules DSL will not bend to work how you want it to. You’ll be much happier using any of the other languages.