# Use rollershutter state in rule

**URL:** <https://community.openhab.org/t/use-rollershutter-state-in-rule/43918>\
**Category:** Scripts & Rules\
**Created:** [April 20, 2018, 4:05pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918 "2018-04-20T16:05:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![KraxelHuber](https://community.openhab.org/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@KraxelHuber](https://community.openhab.org/u/KraxelHuber)\
**Post date:** [April 20, 2018, 4:05pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/1 "2018-04-20T16:05:47Z")

</div>

I am using a rule that closes a number of rollershutters to 85% if the temperature rises above 24 degrees. They will be closed completely if the temperature rises above 30 degrees. I am checking every 30 mins between 11am and 4pm. Once the rollershutter are (partly) closed, I would like to avoid a shutting down command to be sent again. However, the check whether the rollershutter group is (partly) closed doesn’t seem to work correctly.

Here’s the rule:

```javascript
rule "Close rollershutter in first floor if too hot"
when
    Time cron "0 0/30 11-16 ? * * *" // Every 30 minutes between 11h-16h
then
    if (vWeatherTemperature.state >= 30 && g_og_rollershutter.state != 100) {
        g_og_rollershutter.sendCommand(DOWN)
    } else if (vWeatherTemperature.state >= 24 && g_og_rollershutter.state == 0) {
        g_og_rollershutter.sendCommand(85)
    }
end

```

The command to close the group’s rollershutters to 85% will be sent every 30 minutes. Why is it this way?

---

<div class="post-metadata">

**Author:** ![vzorglub](https://community.openhab.org/user_avatar/community.openhab.org/vzorglub/32/17166_2.png) [@vzorglub](https://community.openhab.org/u/vzorglub)\
**Post date:** [April 20, 2018, 4:21pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/2 "2018-04-20T16:21:50Z")

</div>

I don’t quite get what you want to do.  
The first if statement is clear “IF (TEMP\>=30 AND SHUTTER OPEN)”  
Your second if statement reads: “IF (TEMP\>=24 AND SHUTTER CLOSED)”  
That will not be true unless it was 30+ BEFORE  
But if it’s only 26 at 11am your rule won’t do anything  
Check if the roller shutter is NOT partly closed (85%) instead  
“IF (TEMP \>= 24 AND SHUTTER NOT PARTLYCLOSED)”

```php
} else if (vWeatherTemperature.state >= 24 && g-og-rollershutter.state != 85) {

```

Does that make sense? Does it solve your issue?

---

<div class="post-metadata">

**Author:** ![opus](https://community.openhab.org/user_avatar/community.openhab.org/opus/32/55801_2.png) [@opus](https://community.openhab.org/u/opus)\
**Post date:** [April 20, 2018, 6:55pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/3 "2018-04-20T18:55:13Z")

</div>

I would use logInfo statements in the rule in order to see which line of code gets hit and which not.  
Secondly I would initially separate the checks in each if statement in order to get a clear picture which check does work ( make two if statements instead of a combined check).  
I suspect the group state is not what you think it is. How did you define the group?

---

<div class="post-metadata">

**Author:** ![KraxelHuber](https://community.openhab.org/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@KraxelHuber](https://community.openhab.org/u/KraxelHuber)\
**Post date:** [April 20, 2018, 7:24pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/4 "2018-04-20T19:24:53Z")

</div>

> [@vzorglub](#):
>
> I don’t quite get what you want to do.

That’s simple:

1. If it’s more than 24°C, I want the rollershutter to be partially closed (85%).
2. If the temperature rises further above 30°C, I want the rollershutter to be completely closed (100%):
3. This should only happen between 11am and 4pm.

> [@vzorglub](#):
>
> The first if statement is clear “IF (TEMP\>=30 AND SHUTTER OPEN)”

Not quite, it should read: “IF (TEMP\>=30 AND SHUTTER ARE NOT COMPLETELY CLOSED)"

> [@vzorglub](#):
>
> Your second if statement reads: “IF (TEMP\>=24 AND SHUTTER CLOSED)”

No, in my view it reads: “IF (TEMP\>=24 AND SHUTTER **OPEN** )”

> [@vzorglub](#):
>
> But if it’s only 26 at 11am your rule won’t do anything

Yes, it will!

---

<div class="post-metadata">

**Author:** ![KraxelHuber](https://community.openhab.org/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@KraxelHuber](https://community.openhab.org/u/KraxelHuber)\
**Post date:** [April 20, 2018, 7:38pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/5 "2018-04-20T19:38:09Z")

</div>

> [@opus](#):
>
> I would use logInfo statements in the rule in order to see which line of code gets hit and which not.

These days, the “else if” part hat been correctly executed as the temperature rose above 24°C but not above 30°C (between 11am and 4pm). However, this rule has been executed every 30 minutes (I originally placed a telegram msg into the code but omitted it when posting).

> [@opus](#):
>
> Secondly I would initially separate the checks in each if statement in order to get a clear picture which check does work ( make two if statements instead of a combined check).

Good idea. Will do.

> [@opus](#):
>
> I suspect the group state is not what you think it is. How did you define the group?

That’s exactly what I think, hence the title of this post 😉 I suspect that openHAB interpretes the rollershutter.state part always as TRUE even when it’s actually not.

g\_og\_rollershutter.state == 0 should be true if all rollershutters are completely open. However, after the first execution of the rule, the rollershutters are closed (85%), i.e. I would expect rollershutter.state == 85. I wonder why this rule still executes?

The group is defined as follows:

```auto
Group:Rollershutter:OR(100,0) g_og_rollershutter "Rollläden Obergeschoss" <blinds>

```

Maybe the error is located in this very definition.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [April 20, 2018, 8:11pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/6 "2018-04-20T20:11:43Z")

</div>

I find that it is a little more clear when writing rules like this to structure it a little bit differently:slight\_smile:

1. determine if you need to run at all and if not exit
2. calculate what needs to be done
3. do it

So the rule becomes:

```php
rule "Close rollershutter in first floor if too hot"
when
    Time cron "0 0/30 11-16 ? * * *" // Every 30 minutes between 11h-16h
then
    // 1. determine if you need to run at all and if not exit
    if(vWeatherTemperature.state <= 24) return;

    // 2. calculate what needs to be done
    var State newState = if(vWeatherTemperature.state > 30) DOWN else new DecimalType(85)

    if(newState == DOWN && g_og_rollershutter.state == 100) return;
    if(newState instance of DecimalType && g_og_rollershutter.state != 0) return;

    // 3. do it
    g_og_rollershutter.sendCommand(newState)
end

```

Admittedly this is a few more lines of code, but with this approach, you avoid the long run on if statement conditionals and therefore it is much easier to debug.

The theory of operation is:

- if the temp is less than 24 just exit because there is nothing to do
- calculate the new state to send as a command to the rollershutter. Because we already know the temp is over 24 we only need to test to see if the temp is over 30 and choose between DOWN or 85 as appropriate
- calculate whether we should send newState as a command or not based on the current state of the rollershutter. If we should not send the command, exit the Rule here
- finally, if we got this far we know what command to send and we know we need to send it so we do it

---

<div class="post-metadata">

**Author:** ![KraxelHuber](https://community.openhab.org/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@KraxelHuber](https://community.openhab.org/u/KraxelHuber)\
**Post date:** [April 22, 2018, 5:53pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/7 "2018-04-22T17:53:37Z")

</div>

Hi Rich, thanks a lot for your input. I do see the advantages of using your strategy which is clearly superior.

However, I am struggling a bit when using your code (copy & paste). Using VS Code, I get a number of errors.

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/c/3/c3c8225048eb646262473283f082a33b8c3b0c51.png)

Also, would you mind to elaborate a bit on the syntax?

- new DecimalType(85)
- instance of DecimalType

Is ‘newState’ a variable or is it an instance of an object? I don’t fully understand.

Thanks a lot. 🙂

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [April 22, 2018, 7:49pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/8 "2018-04-22T19:49:21Z")

</div>

Hmm. All the errors are related to the same problem. I guess State isn’t available. Try replacing State with Command.

```
var Command newState = if(vWeatherTemperature.state > 30) DOWN

```

If that doesn’t work try removing the type entirely.

```
var newState = if(vWeatherTemperature.state > 30) DOWN

```

If that doesn’t work try using 100 instead of DOWN.

```
var DecimalType newState = if(vWeatherTemperature.state > 30) 100

```

Or perhaps it needs to be a PercentType.

> [@KraxelHuber](#):
>
> Is ‘newState’ a variable or is it an instance of an object?

Yes. Anytime you call `new` it creates an Object. Anytime you see var or val you are defining a variable. So that line is creating an instance if an object and assigning it to a variable.

`instance of` returns true if there last operand is the toe of the right operand.

---

<div class="post-metadata">

**Author:** ![KraxelHuber](https://community.openhab.org/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@KraxelHuber](https://community.openhab.org/u/KraxelHuber)\
**Post date:** [April 22, 2018, 8:17pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/9 "2018-04-22T20:17:11Z")

</div>

> [@rlkoshak](#):
>
> if(newState instance of DecimalType && g\_og\_rollershutter.state != 0) return;

This line of code is producing error messages no matter which of your suggestions are used. I now tried the following solution, but I am not sure if this is all programmatically correct.

```auto
rule "Close rollershutter in first floor if too hot"
when
    Time cron "0 0/30 11-16 ? * * *" // Every 30 minutes between 11h-16h
then
    // 1. determine if you need to run at all and if not exit
    if(vWeatherTemperature.state <= 24) return;

    // 2. calculate what needs to be done
    var newState = if(vWeatherTemperature.state > 30) 100 else 85

    if(newState == 100 && g_og_rollershutter.state == 100) return;
    if(newState == 85 && g_og_rollershutter.state != 0) return;

    // 3. do it
    g_og_rollershutter.sendCommand(newState)
end

```

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [April 22, 2018, 8:21pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/10 "2018-04-22T20:21:01Z")

</div>

The best way to find out if it works is to run it.

It looks ok to me.

---

<div class="post-metadata">

**Author:** ![KraxelHuber](https://community.openhab.org/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@KraxelHuber](https://community.openhab.org/u/KraxelHuber)\
**Post date:** [April 22, 2018, 8:26pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/11 "2018-04-22T20:26:52Z")

</div>

I am now inserting telegram msgs after each line to learn what’s really going on. I’ll post the results the other day. 🙂

---

<div class="post-metadata">

**Author:** ![KraxelHuber](https://community.openhab.org/letter_avatar_proxy/v4/letter/k/4bbf92/32.png) [@KraxelHuber](https://community.openhab.org/u/KraxelHuber)\
**Post date:** [April 28, 2018, 7:15pm UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/12 "2018-04-28T19:15:11Z")

</div>

Here’s my current code. I think it’s working, but I need some more tests.

```auto
rule "Close rollershutter in first floor if too hot"
when
    Time cron "0 0 10-16 ? * * *" // Every 60 minutes between 10h-16h
then
    // 1. determine if you need to run at all and if not exit
    if (vWeatherTemperature.state < 24) {
        logInfo("test.rules", "Current temperature (" + vWeatherTemperature.state.toString + "°C) is less than 24°C. No further action.")
        return;
    }
    
    // 2. calculate what needs to be done
    var Number newState
    if (vWeatherTemperature.state > 28) {
        newState=100
    } else {
        newState=85
    }
    logInfo("test.rules", "The new closing level is " + newState + "%.")

    if (newState == 100 && og_hanna_aRollade.state == 100 && og_luisa_aRollade.state == 100 && og_sz_aRollade.state == 100) {
        logInfo("test.rules", "Rollershutters have already been closed. No further action")
        return;
    }
    if (newState == 85 && og_hanna_aRollade.state != 0 && og_luisa_aRollade.state != 0 && og_sz_aRollade.state != 0) {
        logInfo("test.rules", "Rollershutters have already been partly closed. No further action")
        return;
    }

    // 3. do it
    g_og_rollershutter.sendCommand(newState)
    logInfo("test.rules","Rollershutter have been closed to" + newState + "%.")
    sendTelegram("bot_andi", "Rollershutter have been closed to " + newState + "%.")
end

```

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [April 29, 2018, 2:37am UTC](https://community.openhab.org/t/use-rollershutter-state-in-rule/43918/13 "2018-04-29T02:37:40Z")

</div>

If you define g\_og\_rollershutter as Group:Number in then you can just check the state of the Group instead of the state of each rollershutter separately.

When you see repeated code, there is usually a way to avoid the repetitions which results in cleaner coffee which is easier to read and understand and modify.
