[SOLVED] Telegram rule

Just getting started with OH2 and trying to test a telegram bot message when a door sensor changes to open. /etc/openhab2/rules/telegram.rules looks like this:

rule “Send telegram with Formatted Message”
when
Item zwave:device:xxxxxxxx:node10 changed to OPEN
then
sendTelegram(“botname”, “Front door opened”)
end

When I restart openhab I get this in the log:

Configuration model ‘telegram.rules’ has errors, therefore ignoring it: [3,2]: no viable alternative at input ‘:’

I’m pretty sure I’m missing something simple, just having a hard time finding good examples to learn from.

Have you tried using the Item name? Also the quotes need to be different (use a text editor for writing rules, VSCode with OH extension.:wink:).

Example:

rule "Send telegram with Formatted Message"
when
    Item Name_of_item changed to OPEN
then
    sendTelegram("botname", "Front door opened")
end

Yep, I tried this:

rule “Send telegram with Formatted Message”
when
Item “Front Door” changed to OPEN
then
sendTelegram(“botname”, “Front door opened”)
end

I’m editing with vim.
And still getting an error:

2019-02-23 12:55:19.393 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘telegram.rules’ has errors, therefore ignoring it: [3,5]: no viable alternative at input ‘“Front Door”’

It’s the quote marks. I changed the item’s name to Front_Door, edited the script to have the name without quotes and the rule loads now. But I’m getting a different error now.

2019-02-23 13:07:15.666 [ERROR] [internal.handler.ScriptActionHandler] - Script execution failed: ReferenceError: “sendTelegram” is not defined in at line number 1

Have you installed the telegram binding via PaperUI ?

I don’t see a telegram binding, I do have a telegram action installed.

Have a look at VSCode with the OH extension, you can thank me later.:wink:

I was referring to the overall look of the quotes posted in the original rule. The solution to the first issue was to use the Item name in the when section of the rule. Had nothing to do with quotes per say.

Have you setup the the telgram.cfg file and defined “botname”?

I did setup /opt/openhab2/services/telegram.cfg:

bots=botname

botname.chatId=redacted
botname.token=redacted

I appreciate the tip for VSCode but I really prefer to use vim. Is there something that vim won’t do that VScode will?

VS Code with the openhab extension will syntax highlight for starters and help you write valid rules, also does a drop down feature that will suggest things/items/etc from your OH. https://marketplace.visualstudio.com/items?itemName=openhab.openhab << Example animation of VS Code in use.

As for quotes around the item name in the when statement quotations are not needed, copy and paste your item name as it is in your items file or PaperUI depending on how you set the item up.
Example:

rule "Garage lights when its dark and garage door is closed"
when
  Item door_to_garage_con changed from CLOSED to OPEN
then
  sendTelegram("bot1", "door has been opened")
end

Per the doc’s token authentication is required.

Got it working this morning. Had to change the item to the more discreet door sensor rather than the thing. Not sure I’m using the right verbiage there but it’s working and I understand how to write more rules better now.

I should ask this…

My working rule uses an Item and what I see in the log when the door opens. So now my thought is the physical sensor is the thing and it has multiple items like whether it’s open or closed, battery level etc.

Is that the correct way to think about these?

1 Like

Yes you trigger the rule based on the Item as shown in the second post. You could also trigger the rule using Channel.
Example:

rule "Send telegram with Formatted Message"
when
Channel zwave:device:xxxxxxxx:node10 changed to OPEN
then
sendTelegram("botname", "Front door opened")
end
1 Like

Interesting, I never thought about triggering off at the thing level.

Only downside I can think of is if you want to be notified on multiple doors via group it would require all things under when…unless I also missed group things with recent releases, which is very possible.