Automatic Shading Rule

(Peter Aschinger) #1

Dear OpenHabers,

i use a rule here for automatic close and open rollershutters.

Talking about the open after the sun is gone.

the original code is like that:

if (Rolloautomatik_zielwert.state == Integer::parseInt(i.state.toString())) {

it compares the real position of the rollershutter with the settet position like 70
my rollershutter (shelly2.5) does not exact reach this position, it stop -2 or +2 (68 up to 72) due to
tolerances during the movement.
But not my opening rule will not trigger because it´s not on the settet position.
I want to put some “tolerrance” in to the rule, but therefore my knowledge is not enough.
it try a couple of thing but no trigger of the rule
the last i tested was this:

((Rolloautomatik_zielwert.state <= (Integer::parseInt(i.state.toString()) - 3)) && (Rolloautomatik_zielwert.state >= (Integer::parseInt(i.state.toString()) +3))) {

Is there somebody who kindly provide me some support ?
Thanks a lot in advance.
BR Peter


(Rossko57) #2

If you look at your logic, it boils down to
if ( (x=< y) && (x>= y) )
both those things cannot be true at the same time, so the && (and) is never satisfied.

Did you mean || (or)?


(Markus Storm) #3

No but the order he used in comparisons is wrong it should read (x >= y - 3) && (x <= y +3)

PS: Shading automation


(Peter Aschinger) #4

hi rossko57, long time no read :slight_smile:
hi mstormi,

this the first one:

((Rolloautomatik_zielwert.state <= (Integer::parseInt(i.state.toString()) - 3)) && (Rolloautomatik_zielwert.state >= (Integer::parseInt(i.state.toString()) +3))) {

so this is what you suggest?:

((Rolloautomatik_zielwert.state >= (Integer::parseInt(i.state.toString()) - 3)) && (Rolloautomatik_zielwert.state <= (Integer::parseInt(i.state.toString()) +3))) {

My Goal is:
After other parts of the rule send my shutter to for example 70, it reaches its position at 72
(reportet via mqtt)
If the rule wants to open the shutter later, the rule check the position of the shutter.
If its not exact there (70) the rule wont open the shutter, because perhaps it is moved in the meantime
manually (close it complete or open it).
Now it want that the rule “thinks” while the shutter is at -2 (68) or +2 (72) it also at its right position close to 70.
Hoppyfully this works.


(Rossko57) #5

You have all the working parts available. And, Or, Greater, Less, Add offset, Subtract offset.
It’s up to you to assemble them into the logical condition that you want.

So in words, that condition is -

some Item state is more than a target (less a little)
and at the same time
some Item state is less than a target (plus a little)

which is a way of saying, the item state is close to the target.

Sounds like the wrong condition to me? Wouldn’t you want -
if the Item state is not close to the target, do something


(Peter Aschinger) #6

Thank you for your ideas but i don’t have any idea how to write this into a rule with the correct Syntax.


(Rossko57) #7

Formulate the condition you want in words. I’ve given two examples, but have no idea what your rule is doing.

Get your logic straight, and we can fix the syntax.


(Peter Aschinger) #8

Hi rossko57,

so give me another try:
My Shading Rule does the following:
If the Azimuth of the Sun is in the right angle (i can set this), the Rule sends the value that it set in “Rolloautomatik_zielwert” to my Rollershutter Actor with Mqtt (its a Shelly 2.5)
This “Rolloautomatik_zielwert” can be any value from 0 up 100. 0=open, 100=closed.
So for the Shading i use a value of 70.
Later the day if the Elevation of the Sun is in the right angle (i can set this), the Rule sends a 0 to open the Shutter.
But before the rule checks the position of the shutter: is it at “Rolloautomatik_zielwert” 70 ?.
Because probably somebody opened it or closed it manually. This avoids usless moving of the Shutter.

No coming to my problem:
If the Shelly receives the command close to “Rolloautomatik_zielwert” thats 70, it stops most of the time at 71 or 72. This comes from Tolerances.
But now my opening rule wont open, because is comparing the “Rolloautomatik_zielwert” with the real State of the Shutter i think its “==” part in the rule:

if (Rolloautomatik_zielwert.state == Integer::parseInt(i.state.toString())) {

So now i want to change the rule due to: “Open also if the position is “Rolloautomatik_zielwert” but in the range of -3 or +3”

Is this posible ?

Br Peter


(Rossko57) #9

Of course it’s possible. See @mstormi post for the method.


(Peter Aschinger) #10

Ok then,

I will try this:

If ((Rolloautomatik_zielwert.state >= (Integer::parseInt(i.state.toString()) - 3)) && (Rolloautomatik_zielwert.state <= (Integer::parseInt(i.state.toString()) +3))) {

(Peter Aschinger) #11

so this works, thanks a lot!

if((Rolloautomatik_Sued_zielwert.state as Number).intValue <= (i.state as Number).intValue +3 && (Rolloautomatik_Sued_zielwert.state as Number).intValue >= (i.state as Number).intValue- 3) {
1 Like