In OH4 some blocks of contextual info (command, newState, oldState) now have a toString already included.
While this is mostly useful it causes a rule to fail with type-errors if the specific contextual info is undefined.
The (I think pretty common) use case is when a rule has different triggers like commands, changeEvents.
I guess this question mainly goes to the marvelous @stefan.hoehn (who put a lot of work to turn blockly in such a great tool for openhab ): Would it be possible to change the contextual blocks so that they donât fail if evaluated to undefined?
It can be reproduced by a rule with only a contextual block which is triggered manually:
yes, I know that one.
But it doesnât help if a rule is triggered by change and command events because event available is true in both cases.
So I didnât find a clean solution to evaluate the command and newState values.
My current workaround is to directly search within the ctx-context.
Based on my experiments, you should be able to use the âcontextual info [event type]â to tell the difference between a command and changed trigger. When triggered by a command that will be âItemCommandEventâ and when triggered by a change it will be âItemStateChangedEventâ. Depending on the event, there might be more information in âget context attribute âeventââ. The full list of ways the rule is triggered and the values of the event type include:
Trigger
event type
attribute event
received command
ItemCommandEvent
undefined
received update
ItemStateUpdatedEvent
undefined
changed
ItemStateChangedEvent
undefined
Time
TimerEvent
undefined
System event (e.g. runlevel)
undefined
undefined
Manually run
ExecutionEvent
Execution triggered by manual
Run from another rule
ExecutionEvent
Execution triggered by runRuleAction
Further information is available in âget context attribute âeventââ
This isnât arguing for or against anything proposed on this thread. I just wanted to make sure that all were aware that you can see what sort of event triggered the rule and then take different actions as necessary.
oh yes, youâre right. I cannot tell why I missed that.
Maybe (as my excuse) failing with a stacktrace if one uses the contextual info block too carelessly might not be very beautiful.
But of course checking the type before using the state or the command is fine and works as a solution. many thanks
No, thatâs Blockly only I think. I didnât try it in JS Scripting. Iâm not sure ctx even exists outside of Blockly. Iâm pretty certain it doesnât in file based JS Scripting rules for sure.
with the same result on GUI-based rules. Doesnât seem to be consistent if rule based triggers have no context.
Sure, as a workaround one might pass a variable to the rule and check for existence by
if (typeof myVariable !== 'undefined') {}
ctx does provide results in JS scripting for other events though.
What version of OH are you testing with? There have been some changes in what gets populated in event since 4.0 release I think.
In my experiments, the called rule always has event.type defined and itâs always ExecutionEvent. I tested using Blockly above and JS Scripting just now.