Extracting a token from a String Variable

:bulb: Attention, there's a big difference between split and tokenize with such delimiters:
​'laurel and hardy'​.split('and') => [laurel, hardy]
'laurel and hardy'​.tokenize('and') => [l, urel, h, r, y]

Hmmm, maybe it's necessary to give us both possibilities (in case someone uses intentionally such "strange" behaviour)... :thinking:

BTW: Any idea how to count tokens (or to count a substring)?

Yes... How tokenize and split handle multi character delimiters is 100% different - entire string versus each character in string individually. It is why I almost never use tokenize in my non-Hubitat code - it doesn't match the way my mind thinks :).

3 Likes

Wait, no one uses Hubitat to do anything "strange", do they?

4 Likes

Next release includes a new version of Rule, Rule-4.1. So it has no backwards compatibility limits with Rule-4.0. This version uses split() instead of tokenize() for extracting a token from a string.

2 Likes

On purpose, or accidentally because of my own incompetence?

Never mind, either way the answer is yes. :wink:

1 Like

@bravenel
Can You tell us, when the release?

It is released now.

1 Like

Hi All, question from a relative newbie. I've created a rule that reads the date/time into a variable - sets it to "now". I then created a Attribute time that displays the value of that variable which shows in the tile as 2021-05-26T17:56.056-0400. So I of course thought I could use the tokenize to pull the date (delimiter T, position 0) and time (would need 2 steps) out into separate string variables. Alas no luck. Any suggestions would be greatly appreciated. Thanks,

Is it possible to extract a numeric value from a string? Example: string contains “123 Days”. Extract token “123”, then convert to a number 123.

Yes, you can put a numeric string into a Number variable.

In your case you'd might have to extract the numeric string first if its embedded in a longer string, or if you have the string numeric value in a variable, you could drop that in to the above.

Thanks kindly! It took some experimentation and finagling, but I finally got it to work with a token to extract the number string.

1 Like

Just started using this - would it be possible to extract the token directly from a device attribute? At the moment this needs an additional action to pull the data into a variable from the device. Not a big issue if this isn't possible.

RM is at the point where additional features like the one you're requesting are problematic. The app is overly complex as it is, and when it's possible to do something already, I'm very reluctant to add more features to "improve" it.

3 Likes

No problem. It's only an extra line in my rules anyway so not a big deal as you point out.

1 Like

@bravenel Can the delimiter be a variable? I'm trying to use the token feature to get key/value pairs from a string.

It doesn't work if I specify the delimiter as a variable (%strKeyName%). The interface knows it's a variable and shows its value, but the result of the token function is blank (strKeyValue_tmp).

If I directly type in the delimiter value instead of referencing a variable, it works:

With functions that accept string input, it may not be apparent if I can use variable references.

Also too bad there isn't a way for a rule to return a value when called by another rule. Then I can just call this rule a function and pass it a delimited string and key string and get the value of the key.

Also, any guidelines on what characters are supported for delimiters?

  • delimiterText ok
  • (delimiterText) nope
  • {delimiterText} nope
  • 'delimiterText' ok but can be confusing because it may look like a double-quote in gui
  • :delimiterText: settled on this for now
1 Like