Upgrading to OH3 - Rules DSL problem (if statements)

I have upgraded to OH3 and most things have gone fine, including using the new time library in rules but I have a problem that I can’t find a solution to.

The rule below is used to close my roofblinds when the sun shines through them onto the worktop in the kitchen. The rule is triggered each time the Astro binding updates the sun position and it first checks whether the sun is in the position where it shines through the roof window, then it checks is the sun is out (using the solar panels as a proxy for intensity) and finally it checks if the blinds are open.

It worked fine in OH2 but now, it doesn’t pass the first condition, wherever the sun is.

// Roof blind rules

// Declare variable for auto closed flag
var AutoClosed = 0

//  Rules

rule Sunshine
when Item Azimuth received update
then
        logWarn("rules.sunshire", "Rule triggered by azimuth update. Azimuth is: "+Azimuth.state.toString)
        if ((Azimuth.state > 110) && (Azimuth.state <206)) { // If sun is overhead
                logWarn("rules.sunshine", "Azimuth in range. Solar power is: "+Solar_average.state.toString)
                if (Solar_average.state > 1600) { // Close the blinds if the sun is bright
                        logWarn("rules.sunshine", "Solar power in range. Blind position is: "+RoofBlinds.state.toString)
                        if (RoofBlinds.state == 100) { // If the blind is open
                                AutoClosed = 1 // Set flag because the blind was closed automatically by this rule
                                sendCommand(RoofBlinds, DOWN) // and close the blind
                                }
                        }
                if (Solar_average.state <700 && AutoClosed == 1 && RoofBlinds.state != 100) { // Open the blinds in the sun is dim and they were closed automat$
                        sendCommand(RoofBlinds, UP)
                        AutoClosed = 0
                        }
                }
        if (Azimuth.state > 206 && AutoClosed == 1) { // Open blinds if sun is no longer overhead and they were closed automatically
                sendCommand(RoofBlinds, UP)
                AutoClosed = 0
                }
end

This is what appears in the log:

2021-02-27 11:12:23.681 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Solar_average' received command 2268
2021-02-27 11:15:41.295 [WARN ] [hab.core.model.script.rules.sunshire] - Rule triggered by azimuth update. Azimuth is: 162.96620351644566 °
2021-02-27 11:15:41.307 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Azimuth' changed from 160.21833952726723 ° to 162.96620351644566 °
2021-02-27 11:17:23.637 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Solar_average' received command 2268
2021-02-27 11:22:23.581 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Solar_average' received command 2268
2021-02-27 11:25:41.304 [WARN ] [hab.core.model.script.rules.sunshire] - Rule triggered by azimuth update. Azimuth is: 165.74079577034144 °
2021-02-27 11:25:41.315 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Azimuth' changed from 162.96620351644566 ° to 165.74079577034144 °
2021-02-27 11:27:23.527 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Solar_average' received command 2256
2021-02-27 11:27:23.534 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Solar_average' changed from 2268.0 W to 2256.0 W
2021-02-27 11:32:23.485 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Solar_average' received command 2244
2021-02-27 11:32:23.495 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Solar_average' changed from 2256.0 W to 2244.0 W
2021-02-27 11:35:41.292 [WARN ] [hab.core.model.script.rules.sunshire] - Rule triggered by azimuth update. Azimuth is: 168.53843769558236 °
2021-02-27 11:35:41.301 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Azimuth' changed from 165.74079577034144 ° to 168.53843769558236 °
2021-02-27 11:37:23.431 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Solar_average' received command 2256
2021-02-27 11:37:23.454 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Solar_average' changed from 2244.0 W to 2256.0 W
2021-02-27 11:42:23.995 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Solar_average' received command 2244
2021-02-27 11:42:24.027 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Solar_average' changed from 2256.0 W to 2244.0 W
2021-02-27 11:45:41.420 [WARN ] [hab.core.model.script.rules.sunshire] - Rule triggered by azimuth update. Azimuth is: 171.35506087356174 °
2021-02-27 11:45:41.463 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Azimuth' changed from 168.53843769558236 ° to 171.35506087356174 °

RoofBlinds is 100 throughout.
So the rule is triggered but despite the values of the items being within the range of the if statement, the rules don’t work.
I’m guessing something has changed with the evaluation of numeric items that’s making them not work with the if statements but I don’t know what it is. I have the same problem with similar rules including those where the first condition has no logical operator (so I don’t think it’s a problem with the && operator).

Any ideas gratefully recieved/investigated.

Openhabian on RPi3
OH3.0.0

Change your azimuth item type from Number:Angle to Number and see if that helps.

Leave your Item as angle type, but do your comparison with another angle instead of just a number.

1 Like

Thanks, that makes absolute sense. I’ll add dimensions to the comparators for both angle and power and see how it works tomorrow when the sun is in range again.

Not to do with OH2 to OH3 as such, it was that I added dimensions at the same time as I did the upgrade.

OK that’s solved the problem for a temperature based item so we’re on the right lines but how do I do it for Angles and Power?

I took a guess at:

 if ((Azimuth.state > 110|°) && (Azimuth.state <206)|°) { // If sun is overhead

But I get:

no viable alternative at input '|'

Similarly, I guessed:

if (Solar_average.state > 1600W) { // Close the blinds if the sun is bright

but I get:

extraneous input 'W' expecting ')'

I could go back to @LaurentiuB’s suggestion and make the items dimensionless but I’d rather give dimensions to the comparators in the rules if I can.

My solution it’s just a quick dirty fix, @rossko57 solution is way better.
I’m also following this topic to change my rules definition.

@rossko57 I see in the logs that there is an extra space after the numeric value

Item 'Solar_average' changed from 2256.0 W to 2244.0 W
Rule triggered by azimuth update. Azimuth is: 171.35506087356174 °

This space should be also in the rules like this?

if ((Azimuth.state > 110| °) && (Azimuth.state <206)| °)
if (Solar_average.state > 1600| W)

Brilliant, that was it for both angles and power.

Thanks!

The framework adds the space when it does the quantity (numeric & unit) toString thing. It is not present in the actual unit, and not required with the magic pipe |.

var someQuantity = 22.0|W , no space required
someQuantity.toString yields "22.0 W"
someQuantity.getUnit.toString yields "W", no space
2 Likes