Cloudiness from OpenWeatherMap binding

A better approach is to trigger the rule when the cloudiness Item changes. Then in the rule check the time.

I have almost exactly this rule in my config but I’m using Jython rules.

from core.rules import rule
from core.triggers import when

@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("Cloudy Lights",
      description="Turns ON or OFF some lights when it is cloudy during the day",
      tags=["lights"])
@when("Item vIsCloudy changed")
@when("Item TimeOfDay changed")
def cloudy_lights(event):
    """When it's day time, cloudy, and someone is home, turn on the weather
    lights.
    """

    # If it's not DAY or cloudy isn't set, exit.
    if (items["TimeOfDay"] != StringType("DAY") or
        isinstance(items["vIsCloudy"], UnDefType)):
        return

    # If no one is home, turn OFF any lights that are on.
    if items["gPresent"] == OFF:
        [event.sendCommand(light, "OFF") for light in ir.getItem("gLights_ON_WEATHER").members if light.state == ON]
        return

    # Someone is home and it's DAY time, turn ON/OFF the lights based on the cloudiness.
    if event.itemName == "TimeOfDay":
        sleep(0.5)

    cloudy_lights.log.info("It is {} and cloudy changed: {}"
                           .format(items["TimeOfDay"], items["vIsCloudy"]))

    for light in [light for light in ir.getItem("gLights_ON_WEATHER").members if get_key_value(light.name, "Flags", "override") != ON]:
        if items[light.name] != items["vIsCloudy"]:
            events.sendCommand(light, items["vIsCloudy"])

TimeOfDay is defined and populated based on Design Pattern: Simple State Machine (e.g. Time Of Day). There is a reusable library you can download and use at https://github.com/rkoshak/openhab-rules-tools/tree/main/ephem_tod. No coding required.

Notice how I separated the calculation of whether or not it’s cloudy from the rule that acts on it. That lets me have other rule react when it’s cloudy too and I can see whether or not it’s cloud with a simple switch on the sitemap. Also notice how I check whether or not it’s the right time of day to adjust the lights when it becomes cloud.

finally, that last for loop gets at some Item metadata to tell whether or not the light is overridden or not. You can’t do Item metadata from Rules DSL so you could track that information in a Map in the Rule instead. See Design Pattern: Manual Trigger Detection for more on how lights get overridden (i.e. if manually turned on or off they are no longer controlled by the cloudiness until the next day).

Almost. I’m pretty sure it’s:

if(LocalWeatherAndForecast_Current_Cloudiness.state < 90 | %%)

I’m not positive about the double %% though. You need to convert the 90 to a quantity type. The state already is a quantity type.