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

Hello,

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

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!

Just create another rule triggering when this item turns on…

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

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?

Then do this:

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
1 Like

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

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:

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:

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
1 Like

Thank you! I going to try it out tomorrow!

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

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

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

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?

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

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

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:

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

And the log with the additional log entry:

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

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

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

to

    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

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.

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:

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