How to force update of items before calling script

  • Platform information: Windows 11
  • OH: 4.1.1

Hello,

I have DSL code which call external script, but external script read “old” state. Is there possible to force some upgate of items in OH before call external script?

The code seems as following:

vlState = OFF; //or ON
SwitchVirtual_SwitchVirtualAllSwitches.sendCommand(vlState);
logInfo("key pressed", "-------key pressed 8, vlState:{}", vlState.toString()); //<---here is ok
// some force update method of OH??
callScript("Switches-All-OFF.script");
	// //*** called script - inside this script is:
	// var vlState = SwitchVirtual_SwitchVirtualAllSwitches.state.toString();
	// logInfo("22:00 - ALL LIGHTS IN HOME OFF", "time: {}, w.state:{}", vtCurrentTime,vlState.toString()); //<--- here is previous value :-(
	// //***end called script

logInfo("pilot pressed", "pilot pressed - key: {}, vlState: {}", viKey.toString(), vlState.toString());  // <--- here is ok, actual value

moreover - somtime is ok, sometime is not ok, maybe some delay shout be set before calling external scipt to give the time for OH to refresh states?

The short answer is no, there is no way to wait for the Items to change state after receiving a command. Commands are processed in parallel and there is no guarantee that the Item will ever change state in response to the command anyway. And if this Item is a Group Item, as it’s name implies, you don’t have just the one Item that needs to be updated in response to the command (and may never actually change state at all) but all it’s members.

You can add a sleep or make the call happen in a timer to delay the call but there’s no guarantee that the state is the result of the command. If the delay is too short it might still be processing the command. If it’s to long, it may be another command was sent or other interactions changed things in the mean time.

If you want a log statement when this Item changes to OFF, trigger a rule based on that event and put your log statement there. Or, since you know you command the Item to OFF, pass that as an argument to the script and log out that argument.

Ultimately this smells of an XY Problem. This is a really odd thing to put into a script and it’s not clear why it’s in a script.

Hello, thank you for your reply.

I prefere scripts instead of visual methods to build the logic.

I’m using external script as function (called with parameter stored in state of virtual thing).

I will try first with delay, and if it will be not satifying then I will try in another way.

Thank you very much Mr. @rlkoshak for your pricless tips&ticks and big knowledge.

Simple - Thank you!

I’m not sure what “visual” anything has to do with anything. Using a Rules DSL script in this way is weird. It points to an XY Problem.

If you don’t want to use UI rules, don’t. Use .rules files. Calling Rules DSL scripts from a UI script action gives you the worst of both worlds.

Clear, my Master! As you see I still discovering this fantastic environment of OH, and need to learn more from smart people like you!

Perhaps you should post your entire script(s), not just cherry picked, so we can see the overall picture and can give you a better suggestion.