Sending notification through OpenHAB from a python script utilizing 'rules' Rest API calls

Hi all,

I am (ab)using the Rest API to send custom OpenHAB notifications from a Python-script. The script monitors some stuff in my configuration and sends dynamically configured notifications. This is how I managed to get there:

  1. Use the /rules-endpoint to dynamically create a new rule that contains this:
{
  "actions": [
    {
      "id": "1",
      "configuration": {
        "icon": "oh:battery",
        "severity": "high",
        "message": "test message!",
		"userId": "xxxxxxxxxxx@gmail.com"
      },
      "type": "notification.SendExtendedNotification",
      "inputs": {}
    }
  ],
  "uid": "NOTIFICATION",
  "visibility": "VISIBLE"
}
  1. Use the /rules/NOTIFICATION/runnow-endpoint to execute this rule and send the notification
  2. Use the DELETE /rules/NOTIFICATION-endpoint to delete the dynamically created rule

This works flawlessly, but there is one strange thing I encountered, and the solution was to create the rule everytime (step 1) and to delete it every time (step 3). I first tried to use the rules-endpoint to modify the existing rule (with id ‘NOTIFICATION’), but after I used this endpoint once, the ‘rules’-page in the OpenHAB website (I believe it is the HABPanel) does not load anymore and just shows ‘Loading…’

By playing around with my code, I managed to workaround this behaviour by creating, executing and deleting the rule. This does not raise issues with the loading of the ‘Rules’-page on HabPanel.

I am using OpenHAB 4.0.3. Does anybody know why this is happening when ‘updating’ the rule?

Seems to me it would be a lot more straight forward to use a different approach.

  1. Create a String Item, let’s call it Message.
    1. Create a permament rule that gets triggered when ever this Item gets a command.
  2. For the Action use an Inline Script and Blockly to send the message.

image

Note the send notification block will look different for you in OH 4.0. Notifications have been greatly enhanced in OH 4.2 resulting in a more complicated block configuration.

Rules really are not built to be created and deleted in such a dynamic manner.

Alos note that there is a REST API endpoint that I beleive supports calling Actions directly without the need for a Rule. If it’s not finished yet, it will be by 4.3 I expect.

Probably you mean MainUI.

Do you see any errors in the logs? Any errors in the console of the browser?

While the overall approach is a bit overly complex given other approaches, it should still work without breaking the page.

I was going to suggest a very similar approach, but skip the item.

You may have noticed that this endpoint also allows you to send json as a body. This will inject each of the json keys as a variable directly into the rule when it runs. So
image
Will send a notification with whatever you set in message in the request body. For example:

{
  message: 'Test message!'
}

If the users you notify (or any of the other notification settings for that matter) are also dynamic, then this extends easily:
image
and

{
  message: 'Test message!',
  user: 'xxxxxxxxxxx@gmail.com'
}

:+1:

That’s what I get for being in a hurry. :slight_smile: That approach didn’t even occur to me. Thanks for chiming in, that indeed is the best approach.