Parsing an expression
-
wrote on Apr 3, 2021, 5:35 PM last edited by Tarkus Apr 3, 2021, 1:55 PM
If an expression value has a value of 789 can you isolate a character in the expression? i.e. If character 2 = 8 then perform a reaction. If not is this even possible to implement if I put in a feature request?
-
wrote on Apr 3, 2021, 6:02 PM last edited by LibraSun Apr 3, 2021, 4:03 PM
Yes, and there's a straightforward way to do it within a Rule in MSR.
Suppose the variabletemp
= 789 (number) // it could also be "789" (string) and this technique should work fine
Just let another variableextract
be defined by:
substr( temp , 1 , 1 )
// equals 8Substr()
function takes three arguments: a number or string, a (zero-based) starting position, and a number of characters to keep.
Acting ontemp
here, I've asked for a substring starting at position1
(second character) with length1
(so just the "8").See this page for full Docs on using the lexpjs language (more functions coming soon!).
PRO TIP: If you anticipate
temp
may grow to have more digits, but you always wantextract
to pull the "2nd from last digit", use this expression instead, which starts from the right instead:
substr( temp , -2 , 1 )
-
wrote on Apr 3, 2021, 6:31 PM last edited by Tarkus Apr 3, 2021, 2:40 PM
So this would have to be lua on the vera side that populates the extract variable which I can then define as an expression in MSR? I am using the multi-string plugin which gets populated with a 3 digit string on multistrings variable1 and I want to perform 3 different reactions based on the varying values of characters 1 2 and 3. One reaction for each character. It sounds like I would need 3 variables extract1, extract2 and extract3.
-
So this would have to be lua on the vera side that populates the extract variable which I can then define as an expression in MSR? I am using the multi-string plugin which gets populated with a 3 digit string on multistrings variable1 and I want to perform 3 different reactions based on the varying values of characters 1 2 and 3. One reaction for each character. It sounds like I would need 3 variables extract1, extract2 and extract3.
wrote on Apr 3, 2021, 7:22 PM last edited by LibraSun Apr 3, 2021, 3:23 PM@tarkus everything I just wrote was assumed to be on the MSR side not Vera. I was not aware you were referring to a plug-in, and I'm not immediately certain how one would go about doing it on the vera side anyway.
-
wrote on Apr 3, 2021, 7:53 PM last edited by Tarkus Apr 3, 2021, 6:41 PM
It does not have to be on the Vera side. I can get the 3 digit variable into a MSR expression I just don't fully understand how to parse the characters into 3 separate entities for MSR to evaluate in a 3 seperare rules.i do follow what you suggested but just not sure how to implement it in MSR These are for TV channel.IR codes. I am using a lua routine on the Vera side for this and it often causes luup restarts. And I am just looking for a different approach.
-
It does not have to be on the Vera side. I can get the 3 digit variable into a MSR expression I just don't fully understand how to parse the characters into 3 separate entities for MSR to evaluate in a 3 seperare rules.i do follow what you suggested but just not sure how to implement it in MSR These are for TV channel.IR codes. I am using a lua routine on the Vera side for this and it often causes luup restarts. And I am just looking for a different approach.
wrote on Apr 3, 2021, 8:05 PM last edited by@tarkus you would begin by creating a rule and then within that creating the two expressions I mentioned, but from that point on it would be a matter of experimentation to get the other two digits you need in two other expressions.
-
It does not have to be on the Vera side. I can get the 3 digit variable into a MSR expression I just don't fully understand how to parse the characters into 3 separate entities for MSR to evaluate in a 3 seperare rules.i do follow what you suggested but just not sure how to implement it in MSR These are for TV channel.IR codes. I am using a lua routine on the Vera side for this and it often causes luup restarts. And I am just looking for a different approach.
wrote on Apr 3, 2021, 8:05 PM last edited by@tarkus Start by creating a global variable in MSR that uses a getEntity() to fetch the variable contents. Click the
icon and it will build the expression for you when you pick the multistring entity and the attribute that contains the value.
Then, in each rule, create a variable, perhaps called
character
(for my lack of a deeper understanding of its meaning here), and put the expression LibraSun has given you in it. Hit Save. Then add a trigger condition using the Expression Value condition type to test for the character that should match. -
wrote on Apr 4, 2021, 11:08 AM last edited by
Thanks guys, this worked just like I needed it to.
-