Telegram message String to Number value

Hello, i am can’t figure out how to split Telegram message, if i send from Telegram to Openhab name-12.2. Is there possible to split the string value and use both sides. I want to use different name with different number. Like (deviceone-10.11), (devicetwo-11.12).

You can split your message like

var device = TelegramBot_LastMessageText.state.toString.split('-').get(0)
var numbers = TelegramBot_LastMessageText.state.toString.split('-').get(1)

Now you have 2 strings spliitted by -

1 Like

The syntax will be different depending on the rules language you are using. @Baschtlwaschtl shows how to do it in Rules DSL.


Obviously, you’d replace the “the text” with the “get state from Item” block.

In JS Scripting it looks very similar to the Rules DSL. I like to only split the string once but that’s not required. I just find the code easier to read and maintain.

var parts = TelegramBot_LastMessageText.state.split('-);
var device = parts[0];
var number = parts[1];

You can send multiple in one message, you’d just need to split more than once. First split on comma and then split each value in the list on the dash. Note that if you send something like JSON in the first place (which might be awkward in this case) you can directly parse them and use the data like an Object.

var parsed = Json.parse(items.TelegramBot_LastMessageText.state);
for(const element of parsed){
  const device = element.device;
  const number = element.number;

Thank you for examples! I will try.