openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.88k stars 3.59k forks source link

Built-in service-agnostic notification addon using existing dedicated library #11787

Open Redsandro opened 2 years ago

Redsandro commented 2 years ago

Problem

There are some notification addons for services such as Email or Telegram but more and more services keep popping up. As a new user, I found it suboptimal that my first three preferred notification services were not available, and I had to roll my own.

Suggestion

It would make sense to have a single service-agnostic built-in notification addon using a dedicated notification library such as Apprise or Shoutrrr.

Other considerations

Generally, tutorial and documentation-wise, it should be made easy to use private self-hosted notification services such as Matrix. Users should arguably be discouraged from using public services. Arguably, no one should send messages about their presence to third party clouds such as Telegram or in unencrypted fashion such as Email.

Your Environment

openhab-bot commented 2 years ago

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/oh-3-examples-writing-and-using-javascript-libraries-in-mainui-created-rules/108526/55

kaikreuzer commented 2 years ago

using a dedicated notification library such as Apprise or Shoutrrr.

Are you aware of any such library for Java? If so, this could indeed be a nice new add-on. If users have to install a separate application and configure it first, it would also work, but the dedicated add-ons that openHAB already has are then imho much more user-friendly for beginners.

Do you plan to work on such an add-on?

Redsandro commented 2 years ago

Are you aware of any such library for Java?

Not currently, although I'm really not a Java developer. My areas are Python and NodeJS. That's why my first rough thoughts go out to Jython.

Do you plan to work on such an add-on?

Not currently, Java is a limiting factor for me. But with JSScripting and npm support coming to OH, I was contemplating retrofitting some packages when the time comes. It's not ideal, because it would not be an add-on (although I'm free to dream about a NodeJS add-on specifically for writing wrapped ES12 add-ons), so it would only work in scripts, and that would selfishly solve only my own problem.

So I did a quick fix for my immediate problem (which I'll attach below in case it helps others), and in the meanwhile, it seemed like a good idea to bring it up as a possible enhancement. Because I notice some devs with Java knowledge did take the time to make some specific notification add-ons, while in my area of expertise it would make sense to use one of those existing libraries which really takes away hours of work. In the hopes that Java-minded devs might know or find something similar in the Java space, and keep it in mind for next time they choose to implement a different notification service.


My solution for non-supported notifications initially was to use an Email add-on and send mail to a Mailrise server until I wanted to get rid of the mail transport itself. Then I made a simple script-only solution just for Matrix notifications:

/openhab/conf/automation/com.redsandro.openhab/matrix.js

(function(self) {
  'use strict';
  self.sendMatrixMsg = function(room, msg) {
    var HTTP     = Java.type("org.openhab.core.model.script.actions.HTTP")
    var rooms    = {
      home         : '!<roomId>:<host>',
      person1      : '!<roomId>:<host>',
      person2      : '!<roomId>:<host>',
      person3      : '!<roomId>:<host>'
    }
    var roomId   = rooms[room] || rooms['home']
    var token    = '<access_token>'
    var url      = ['https://<host>/_matrix/client/r0/rooms/', roomId, '/send/m.room.message?access_token=', token].join('')
    var payload  = {
      msgtype: 'm.text',
      body: msg
    }

    HTTP.sendHttpPostRequest(url, "application/json", JSON.stringify(payload), 3000)
  }
})(this)

To use in a script:

load('/openhab/conf/automation/com.redsandro.openhab/matrix.js')
this.sendMatrixMsg('home', "I'll turn off the heating in the bathroom.")
lsiepel commented 1 week ago

Looking at the current bindings and services that offer some kind of notification i can see a very wide range of functionaility. They all offer plain text, but some have html or other rich markup. I also see many differences in attachment, buttons, replies, images, video and other capabilities.

Abstracting all those binding behind an interface like what has been done with persistence might be very usefull and reduce the configuration complexity.

As this would require a large development effort, i won't count on this any time soon. In the mean time 4.2.0 brought some more notification possiblities through the openHAB cloud service.