[SOLVED] Can not convert to long value

try to create a rule for Telegram binding on OH 2.5 to reply on ping in same chat, therfore I need to convert chatId to long, which seems I’m not able to.

item definition
String telegramChatId "Telegram ChatId" (gTST) {channel = "telegram:telegramBot:20c8999f:chatId"}


rule "Telegram ping"
  Item telegramMessage received update
  if (telegramMessage.state == "ping") {
    val telegramAction = getActions("telegram","telegram:telegramBot:20c8999f")
    telegramAction.sendTelegram((telegramChatId.state as Number).longValue(), "pong")
    // Could not cast 106878302 to java.lang.Number;
    //telegramAction.sendTelegram((telegramChatId.state as DecimalType).longValue(), "pong")
    // Could not cast 106878302 to org.eclipse.smarthome.core.library.types.DecimalType;
    //telegramAction.sendTelegram(106878302L, "pong")
    // works!

it doesn’t matter if I do as Number or as DecimalType, it just can’t cast it to that type.

what I’m doing wrong here?

Your Item is of String type, so you cannot simply cast it to a Number or DecimalType; you need string to long conversion. Something like this should work:

telegramAction.sendTelegram(Long::parseLong(telegramChatId.state.toString), "pong")

yessss, that dit it. thanks a lot!

1 Like

Glad I could help. I know OH has a somewhat steep learning curve but the rewards when the magic kicks in make it all worthwhile…:grin:
And of course, Happy New Year and welcome to the community!