Using own functions in rules file


I’m using CallMeBot for pushing notifications from OH.
There’s a lot of rules in my.rules which are sending push notifications.

I tried to create a functions to avoid writing the whole command with phone number and key in each line. but i always get parse errors and have no clue if it’s even possible to place functions in rule file

How can use an own function for the main codes of sending a notification and using only one line in a rule to send the message like:

SendPushNotification(“Rauchmelder Buero CO2 Wert:\n” +SZSmokeDetector_Density.state.toString() )

Best Regards,


i am also working to get this going testing using this

function sendNotification(message) {
    actions.get("notification", "notification").sendBroadcastNotification(message);

And then

            sendNotification(something to say);

How do you tried it?


yea i tried it but i get follow error message:

2023-08-06 17:17:42.460 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘my.rules’ has errors, therefore ignoring it: [2,8]: no viable alternative at input ‘function’
[13,2]: missing EOF at ‘SendHttpGetRequest’

i placed the function after the import lines and before a begin of a rule statement

Edith: i did use the wrong rule for testing. sadly it’s still not working.

where i have to place the function to use it in any rules?

This, by definition, means you are using DSL as the rules language. DSL has limited capability for reusable code snippets. Declarations of function will not work as they are not part of the language.

You have a couple of choices:

  1. Stick with DSL and read up on lambdas. You probably want to start here:
  1. Move your rules to any of the other language options which have much better systems for functions.

I highly recommend option #2. The limitations of of DSL are many and if you’re trying for more advanced code like this you will likely run into many more such issues. Even if you are not highly experienced with coding, you have options. The UI Blockly implementation gives you the chance to define your own functions.

I never thought that it will not be easy to switch from Telegram notification to Whatsapp notificaton.
I’m looking since 5 hours to make the my.rules more maintainable withouth putting 3 lines in each rule for using the new notifications

I guess it will be easier to copy an existing binding and customize some parts to wrap the existing function calls with new features because i can’t find any usable examples to create a simple function which will work in my.rules file. Also have no clue how to convert my 3 lines into a Block Library from UI

Do u can give me some tips about the second method about moving to the other language options?

I don’t use Blockly myself, so I don’t have any examples to give you, but there are many examples on the forums right now. You haven’t shown most of the rules you want to convert so no one can give you specific advice.

I don’t know if there’s CallMeBot block library, but you don’t really need that. There is a block for adding in your own custom commands so you can build most of the rule with the basic blocks and then add the one custom script line block for the push notification.

The idea behind Blockly is that there shouldn’t be too much in the way of learning. The blocks are intuitively named and the process of connecting them is entirely point and click.

val urlmessage = URLEncoder::encode("This is a test from openHAB sending you a WhatsApp Message becaue something is happening in your house", 'UTF-8')
   SendHttpGetRequest("" + urlmessage)

Thats the code i want to use in a function. I will check out options again next days.
Thanks for help

Just in case I haven’t made this clear. You cannot just create the function in a different language and then use that with your DSL rule. You would have to convert the entire rule to a new language.

This code, of course, has nothing dynamic in it, so it doesn’t make sense as function. I assume that you mean you would like the function to be able to take the message as an input and send that input message using the WhatsApp action. But in that case, your “function” is only one line long (the SendHttpGetRequest line) and you don’t gain any benefit from using one line in your code to call a one line function.

I didn’t post the entire code which i want to use. This was just an example what are the minimum requirements what needed to send a message.

In the meantime i did wrote a binding plugin which are contains the function. But in current OH version i have to call the actions of a binding before i can use the function. So i also need two lines of code. the only difference is, that i don’t have to add the credentials on each process

In formerly versions it was possible to call the functions from a binding directly without loading it with
getActions() before.