# \[SOLVED\] Wait step in an rule depending on an item state?

**URL:** <https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502>\
**Category:** Scripts & Rules\
**Tags:** rules\
**Created:** [October 13, 2019, 10:31am UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502 "2019-10-13T10:31:48Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [October 13, 2019, 10:31am UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/1 "2019-10-13T10:31:48Z")

</div>

Hello,

i need to optimize the following rule, because of an timing problem.

```php
rule "Media_Wohnzimmer_an"
when
	Item Scenes_Media_Wohnzimmer received command ON or
	Item HM_FL_Taster_T5_Short changed to ON
then
    Zw_steckdose_wohnzimmer_media_Switch.sendCommand(ON)
	createTimer(now.plusSeconds(20)) [RXV775Main_Zone_Zone_channels_Scene.sendCommand('Scene 4')]
	createTimer(now.plusSeconds(21)) [RXV775Main_Zone_Zone_channels_Volume.sendCommand(38)]
end

```

The problem is, that sometimes 20s in the first timer are enough until the network interface of my receiver is ready, but sometimes its even more.  
Now i would like to use a wait step depending on another item state instead of timer, but i dont know how to implement it into the rule.  
The process should be the following:

- Rule is triggert (Scenes\_Media\_Wohnzimmer)
- Electric switch gets turned on (Zw\_steckdose\_wohnzimmer\_media\_Switch)
- Now wait till the state of a new item changed from OFF to ON (Ping state of the network binding)
- Then send scene command to RXV775Main\_Zone\_Zone\_channels\_Scene
- And then, one second later, send volume command to RXV775Main\_Zone\_Zone\_channels\_Volume

Would be great, if you could help me to get this rule optimized!

Thanks, Alex!

---

<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:** [October 13, 2019, 10:51am UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/2 "2019-10-13T10:51:40Z")

</div>

> [@e36Alex](#):
>
> Now wait till the state of a new item changed from OFF to ON (Ping state of the network binding)

Just create another rule triggering when this item turns on…

```php
rule "reveiver turned on"
when
    receiverPingItem changed to ON
then
	RXV775Main_Zone_Zone_channels_Scene.sendCommand('Scene 4')
	createTimer(now.plusSeconds(1), [RXV775Main_Zone_Zone_channels_Volume.sendCommand(38)])
end

```

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [October 13, 2019, 11:34am UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/3 "2019-10-13T11:34:37Z")

</div>

Thanks for your reply, i even thought about that. But this would mean, that every time the switch turns on, the receiver input will be switched to tuner. But there are also other scenes with other input channels.

This would mean, i had to manage different rules and scene states to send the right scene command to my receiver. I thought there maybe would be an easier way just to wait for the network interface of the receiver?

---

<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:** [October 13, 2019, 11:39am UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/4 "2019-10-13T11:39:09Z")

</div>

Then do this:

```php
rule "reveiver turned on"
when
    receiverPingItem changed to ON
then
    if (Scenes_Media_Wohnzimmer.state == ON) {
        RXV775Main_Zone_Zone_channels_Scene.sendCommand('Scene 4')
        createTimer(now.plusSeconds(1), [RXV775Main_Zone_Zone_channels_Volume.sendCommand(38)])
    } else if (anotherSceneItem.state == ON {
        //another scene setting
        //....
    } else if (yetAnotherSceneItem.state == ON {
        //....
    }
end

```

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [October 13, 2019, 11:53am UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/5 "2019-10-13T11:53:17Z")

</div>

Would this way be the only strategy in your opinion or would there be an alternative for the item bases wait step?

At this way i had to configure different rules working in three steps:

1. Step: Triggering the Scene and check if receiver ist ON or OFF:

- if ON trigger the selected scene direktly
- if OFF switch the power switch and receiver to ON

1. Step: Wait for receiverPingItem to switch to ON an then trigger the selected scene
2. Step: Execute the scene setting rule

---

<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:** [October 13, 2019, 12:03pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/6 "2019-10-13T12:03:55Z")

</div>

> [@e36Alex](#):
>
> if ON trigger the selected scene direktly

No necessarily  
You can keep the scenes code inside that one rule above  
Just add another trigger to the rule  
Add an item called `SceneTriggered`  
When you trigger a scene and the received is already ON then do `SceneTriggered.postUpdate(ON)`

Then main rules triggers will be:

```php
rule "reveiver turned on OR scene changed with received already on"
when
    receiverPingItem changed to ON or
    SceneTriggered received update ON
then
    if (Scenes_Media_Wohnzimmer.state == ON) {
        RXV775Main_Zone_Zone_channels_Scene.sendCommand('Scene 4')
        createTimer(now.plusSeconds(1), [RXV775Main_Zone_Zone_channels_Volume.sendCommand(38)])
    } else if (anotherSceneItem.state == ON {
        //another scene setting
        //....

```

This way you keep all your scens code inside ONE rule

Your scene trigger rules will be:

```auto
rule "Media_Wohnzimmer_an"
when
	Item Scenes_Media_Wohnzimmer received command ON or
	Item HM_FL_Taster_T5_Short changed to ON
then
    if (Zw_steckdose_wohnzimmer_media_Switch.state == OFF) {
        Zw_steckdose_wohnzimmer_media_Switch.sendCommand(ON)
    } else if (Zw_steckdose_wohnzimmer_media_Switch == ON) {
        SceneTriggered.postUpdate(ON)
    }
end

```

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [October 13, 2019, 7:23pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/7 "2019-10-13T19:23:49Z")

</div>

Thank you! I going to try it out tomorrow!

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [October 14, 2019, 7:06pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/8 "2019-10-14T19:06:35Z")

</div>

Today i tried it out but there still seems to be an mistake in my rule.  
The first trigger (Scenes\_Media\_Wohnzimmer or HM\_FL\_Taster\_T5\_Short) works fine in fires the first if-condition (switching ON the power switch, if its OFF).

But the second trigger (NetworkDeviceYamahaAVR\_Online) fires the first if-condition instead of the second, too. Even if the conditions are both true.

Can you find the mistake?

Thanks, Alex

```php
rule "Media_Wohnzimmer_an"
when
	Item Scenes_Media_Wohnzimmer received command ON or
	Item HM_FL_Taster_T5_Short changed to ON or
    Item NetworkDeviceYamahaAVR_Online changed to ON
then
	if (Zw_steckdose_wohnzimmer_media_Switch != ON) {
    Zw_steckdose_wohnzimmer_media_Switch.sendCommand(ON)
	}
    else if ((NetworkDeviceYamahaAVR_Online == ON) && (Scenes_Media_Wohnzimmer == ON)) {
	RXV775Main_Zone_Zone_channels_Scene.sendCommand('Scene 4')
	createTimer(now.plusSeconds(3)) [RXV775Main_Zone_Zone_channels_Volume.sendCommand(38)]
	createTimer(now.plusSeconds(8)) [Scenes_Media_Wohnzimmer.postUpdate(NULL)]
	createTimer(now.plusSeconds(8)) [Scenes_Wohnzimmer_aus.postUpdate(NULL)]
    }
end

```

---

<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:** [October 14, 2019, 7:20pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/9 "2019-10-14T19:20:22Z")

</div>

> [@e36Alex](#):
>
> Can you find the mistake?

Yes you are trying to do everything in one rule  
You need rules for scene triggers  
And you need one BIG rule for the scene settings themselves. See my post above

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [October 14, 2019, 7:25pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/10 "2019-10-14T19:25:02Z")

</div>

ok, but can you help me to unterstand why it doesnt work?

my expectation was, that at the second run the else if condition would be fired, because the first condition isnt true. why is the first condition fired in spite of the condition isnt true?

---

<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:** [October 14, 2019, 7:30pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/11 "2019-10-14T19:30:24Z")

</div>

Add a logInfo  
I suspect that the rule runs BEFORE the state of the item is updated. It takes some time for a command being sent to the item state to update. Everything goes through the event bus and it takes a little bit of time.  
If your rule execute straight away the state might not have updated yet.  
Please fix your indents they are all over the place. Use 4 spaces instead of TAB. But not both

```php
rule "Media_Wohnzimmer_an"
when
    Item Scenes_Media_Wohnzimmer received command ON or
    Item HM_FL_Taster_T5_Short changed to ON or
    Item NetworkDeviceYamahaAVR_Online changed to ON
then
    logInfo("TEST", Zw_steckdose_wohnzimmer_media_Switch.state.toString)
    if (Zw_steckdose_wohnzimmer_media_Switch != ON) {
        Zw_steckdose_wohnzimmer_media_Switch.sendCommand(ON)
    }
end

```

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [October 14, 2019, 7:36pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/12 "2019-10-14T19:36:12Z")

</div>

Here is the log. There is not much time between the actions. But the item update (and the trigger of the second run) comes before the second ON command to the power switch:

```php
20:48:53.543 [INFO] [smarthome.event.ItemCommandEvent] - Item 'Scenes_Media_Wohnzimmer' received command ON
20:48:53.545 [INFO] [smarthome.event.ItemStateChangedEvent] - Scenes_Media_Wohnzimmer changed from NULL to ON
20:48:53.553 [INFO] [smarthome.event.ItemCommandEvent] - Item 'Zw_steckdose_wohnzimmer_media_Switch' received command ON
20:48:53.555 [INFO] [arthome.event.ItemStatePredictedEvent] - Zw_steckdose_wohnzimmer_media_Switch predicted to become ON
20:48:53.560 [INFO] [smarthome.event.ItemStateChangedEvent] - Zw_steckdose_wohnzimmer_media_Switch changed from OFF to ON
20:49:45.450 [INFO] [smarthome.event.ItemStateChangedEvent] - NetworkDeviceYamahaAVR_Online changed from OFF to ON
20:49:45.457 [INFO] [smarthome.event.ItemCommandEvent] - Item 'Zw_steckdose_wohnzimmer_media_Switch' received command ON
20:49:45.460 [INFO] [arthome.event.ItemStatePredictedEvent] - Zw_steckdose_wohnzimmer_media_Switch predicted to become ON

```

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [October 14, 2019, 7:49pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/13 "2019-10-14T19:49:11Z")

</div>

And the log with the additional log entry:

```php
21:45:38.087 [INFO] [smarthome.event.ItemStateChangedEvent] - NetworkDeviceYamahaAVR_Online changed from OFF to ON
21:45:38.088 [INFO] [g.eclipse.smarthome.model.script.TEST] - ON
21:45:38.099 [INFO] [smarthome.event.ItemCommandEvent] - Item 'Zw_steckdose_wohnzimmer_media_Switch' received command ON

```

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [December 27, 2019, 3:46pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/14 "2019-12-27T15:46:01Z")

</div>

Hello,

today i took some time and tried again to get this rule running - with no success.

But i found out something strange that maybe could help to find a solution:  
I changed the rule from

```auto
    if (Zw_steckdose_wohnzimmer_media_Switch != ON) {
        Zw_steckdose_wohnzimmer_media_Switch.sendCommand(ON)
    }

```

to

```auto
    if (Zw_steckdose_wohnzimmer_media_Switch == OFF) {
        Zw_steckdose_wohnzimmer_media_Switch.sendCommand(ON)
    }

```

The result is, that nothing happens. Of course, i checked the state of Zw\_steckdose\_media\_Switch and it was OFF. So it seems like something is wrong in the if-definition, but i cant find the mistake.

Hope you can help me.

(I even tried to split it into two rules, but i got the same problem with the if-condition)

Thanks, Alex

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [December 27, 2019, 4:35pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/15 "2019-12-27T16:35:09Z")

</div>

Did you notice that you logInfo the .state of your Item, but try to a comparison with the whole Item object?  
You’ll want to be comparing the .state as well.

You are still at risk of getting the “old” state, before the command has had any effects.

---

<div class="post-metadata">

**Author:** ![e36Alex](https://community.openhab.org/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@e36Alex](https://community.openhab.org/u/e36Alex)\
**Post date:** [December 27, 2019, 5:16pm UTC](https://community.openhab.org/t/solved-wait-step-in-an-rule-depending-on-an-item-state/83502/16 "2019-12-27T17:16:38Z")

</div>

Thanks for this hint!

I checked the rules an added .state at some points. Now it works fine! Thanks!

Also i found out, that the ping at the yamaha avr isnt the best trigger for the “second run” of the rule. Perfect trigger seems to be, when the thing gets online.

Here is the functional rule-code:

```auto
rule "Media_Wohnzimmer_an"
when
	Item Scenes_Media_Wohnzimmer received command ON or
	Item HM_FL_Taster_T5_Short changed to ON or
    //Item NetworkDeviceYamahaAVR_Online changed to ON
	Thing "yamahareceiver:yamahaAV:5f9ec1b3_ed59_1900_4530_00a0dea3719a" changed to ONLINE

then
	if (Zw_steckdose_wohnzimmer_media_Switch.state != ON) {
    Zw_steckdose_wohnzimmer_media_Switch.sendCommand(ON)
	}
    else if ((NetworkDeviceYamahaAVR_Online.state == ON) && (Scenes_Media_Wohnzimmer.state == ON)) {
	createTimer(now.plusSeconds(1)) [RXV775Main_Zone_Zone_channels_Scene.sendCommand('Scene 4')]
	createTimer(now.plusSeconds(3)) [RXV775Main_Zone_Zone_channels_Volume.sendCommand(38)]
	createTimer(now.plusSeconds(5)) [Scenes_Media_Wohnzimmer.postUpdate(NULL)]
	createTimer(now.plusSeconds(5)) [Scenes_Wohnzimmer_aus.postUpdate(NULL)]
    }
end

```
