Project

General

Profile

A "genuine" IF-statement for the Event Script Editor (only performing either the relevant true or false expression)

Added by TechNovation almost 5 years ago

Hi All,

I've been puzzling a bit with the Events in Domotiga and now gradually getting more used to the "way of working". One "big"challenge was figuring out how to use the

IF(<Cond>,<TrueExpr>,<FalseExpr>)
function.

For example the simple action script of:

IF(Dev_21_Value1=="Motion",Device.SetDevice(12,"On"),Device.SetDevice(12,"Off"))
would not do what I wanted it to do (it always turned off device 12, irrespective of Dev_21_Value1). After some testing and looking at the internet I found the answer to the "strange" behavior and apparently I wasn't the first to stumble on this:

[[https://www.domotiga.nl/boards/1/topics/1042?r=2103]]

Apparently the "If"-function used in the script editor of Gambas will evaluate both the True and False Expression of the If-statement irrespective of the condition (and thus in my example will turn device 12 on and off....).
This is not a bug, it is simply how the function works in Gambas: [[http://gambasdoc.org/help/lang/iif?nl]]

As an alternative using normal Gambas routine of IF...THEN..ELSE...ENDIF is not possible within Domotiga script editor as it uses the Gambas "eval" function, which can only handle expressions...supported by this conversation:
[[http://gambas.8142.n7.nabble.com/Eval-Gambas2-td7803.html]]

However I hate to give up on this as it would provide additional freedom and implementation advantages...so I tortured the grey matter a bit more....

What I came up with as a proposed solution:
What I noticed is that in the Script editor it was possible to use different functions which were defined in the Domotiga code already (like Devices.SetDevice), so I wondered why I could not just add my own function which simply performs the "genuine" IF...THEN...ELSE....ENDIF routine.

I implemented the routine and after some tinkering it actually works !!:)

So now I can use the following in the Script Editor -

Events.If_statement(<cond>,<TrueExpr>,<FalseExpr>)
. Which in real life would look like this (example):

Events.If_statement(Dev_21_Value1=="Motion","Device.SetDevice(12,\"On\")","Device.SetDevice(12,\"Off\"))")

Now it truly only performs either the true or false expression depending on how 'Dev_21_Value1 = "Motion"' evaluates
The only drawback (which I would happily accept) is, as you might have noticed the requirement of both expressions being inserted as strings (else the eval function will start evaluating both lines again turning the light on and off again), which requires additional "\" characters for any " in the string.

As an alternative one could also define the true and false expressions as macro's in that case there is no issue with the " and it would look like:

Events.If_statement(Dev_21_Value1=="Motion",Macro_True,Macro_False)

Even though many of the actions requiring an if-routine can be solved with typical solutions as shown in the first hyperlink, I do believe that the proposed "genuine" if-then-else routine can add extra flexibility/possibilities to the script editor.

I hope other people also see the value and that it will be incorporated in a future version of Domotiga. Of course I have no objection if one would like to give the function a different name (e.g. Action.SingleExprIF)or somewhat different syntax...this was just my first attempt at it.

I attached the required modification as an attachment...hope this helps reducing implementation effort if one is willing to do so.

Implementation_Events_If_statement (1.15 KB) Implementation_Events_If_statement Tested Implementation of proposed "true" IF-statement for script editor

Replies (7)

RE: A "genuine" IF-statement for the Event Script Editor (only performing either the relevant true or false expression) - Added by Alexie almost 5 years ago

Yes, the Gambas IIF statement always evaluates both True/False expressions ;-( I will have a look in the "IF" statement used in DomotiGa and see it uses a proper "if then else" ... Else we need to implement something you stated above.

RE: A "genuine" IF-statement for the Event Script Editor (only performing either the relevant true or false expression) - Added by danfos almost 5 years ago

@TechNovation: Good investigation and very clear documented!

Would be good if you can add something to the Events page on this.

On the proposed solution I see two alternatives:
  1. Change the Domotiga code such that it does scan what goes into the eval and if it recognizes a "If", do not use eval but handle the string using some internal code
  2. Add a action type "If"

I would vote for #2.

RE: A "genuine" IF-statement for the Event Script Editor (only performing either the relevant true or false expression) - Added by TechNovation almost 5 years ago

@Danfos: Thanks for compliments and your suggestions.

I was indeed thinking about adding some stuff to the Events page with some more examples and including some comment how the "current" if-function works in a script. However I first have to figure out how I can do this (I did read somewhere that all members can edit the domotiga site .... but probably there are some "rules" (!) involved).

With regard to your 2 suggested "alternatives", I unfortunately have to vote against your preferred nr 2 (sorry...) for the following reason:

Alternative 2 to my opinion does not completely fit in the current layout of the Action types already present. The ones in there all seem to be "single" actions requiring no scripting language. If you would add the If-statement as an action type you would want to typically add "script" language in the boxes with this action type like:
  • textbox 1 containing the condition which is typically is a script like expression like: Dev_20_Value1 == "No Motion"
  • textbox 2 containing the true expression, again a script expression like: Devices.SetDevice(12,"On")
  • textbox 3 containing the false expression, again a script expression like: Mail.SendMail("some Subject","Some Body text",").

While the current script editor has most of these features to compose a script expression already available by a simple mouseclick, the action type window (currently) has none of these features. Therefore in my opinion it best fits in the script editor...

Ultimately I would love to see it implemented similar to the currently available expressions in the script editor like:
Devices.SetDevice(<DeviceId>,<Value>), something that I would love to see fixed in my current implementation is the required "corrections" in case there is a string within the true/false expression string in the my proposed if-function. I did not yet find a way how to do this other than by using a macro.

Hope my argumention is a bit convincing :) ...at least thanks for your suggestions.

RE: A "genuine" IF-statement for the Event Script Editor (only performing either the relevant true or false expression) - Added by danfos almost 5 years ago

On editing the Wiki, yes AFAIK every registered user can edit the Wiki.

Editing is I think not too difficult, just have a look at existing "code", press the ? icon to get some basic help and see RedmineTextFormatting for the finer details. Use Preview to view what you changed and .. there is History so things can be reverted.

Yes, I see the problem you describe with the "If action type" and thinking about it again, there are already Conditions...

While you can not use conditions for the "true action" and "false action" you can have two events:
  • #1 with the normal trigger, the condition and the "true action"
  • #2 with the normal trigger, the inverted action and the "false action"

That looks to me like a much cleaner solution.

RE: A "genuine" IF-statement for the Event Script Editor (only performing either the relevant true or false expression) - Added by TechNovation almost 5 years ago

Ok, thanks for the tips on editing the Wiki. I'll have a go at it pretty soon I hope.

Indeed you're right that many of the If-routine actions could be resolved in alternative ways by using multiple events, conditions and actions.
However I think that in certain cases it can be cleaner if we have a real IF-statement available in the script editor which can prevent the work-around of using multiple events, conditions and actions to achieve the same result.

RE: A "genuine" IF-statement for the Event Script Editor (only performing either the relevant true or false expression) - Added by danfos almost 5 years ago

However I think that in certain cases it can be cleaner if we have a real IF-statement available in the script editor which can prevent the work-around of using multiple events, conditions and actions to achieve the same result.

I am not so sure things are cleaner with a real IF-statement. Yes, instead of one event consisting of one trigger and one "If action" you need two events, with the same trigger, two conditions that are the inverse of each other and two actions instead of one "If action".

What is good about doing it with two events is that you can verify the parts and that it is easy to get an overview.
The "If action" does not really fit in the picture of:

Things are architectured such that an event is executed when it is triggered and (optional) condition(s) are met and resulting in one or more action(s) to be executed.

In the end I know that opinions on what is clean differ, but I hope you give the the Event/Condition/Action option a try :)

RE: A "genuine" IF-statement for the Event Script Editor (only performing either the relevant true or false expression) - Added by TechNovation almost 5 years ago

In the end I know that opinions on what is clean differ, but I hope you give the the Event/Condition/Action option a try :)

Of course I use the Event/Condition/Action "way of working" :) , actually that is the common way that I try to perform most of the events. I only have the stubborn opinion :D that in some cases I oncountered it was cleaner (indeed beauty is all in the eye of the beholder ;) ) if there was a "true" if-statement in the script editor.

By the way I gave it my first attempt to edit the Wiki in the Scripts section. Hopefully it will help out some other Domotiga users.

    (1-7/7)