MQTT notification config example

Could someone show me an example of the MQTT notification config?
I understand from the component page that you have to call the service, however, It’s unclear to me precisely how (and all my tests failed so far).

I’d like to define it as a notifier (static topic off course), and just be able to call that notifier from anywhere else with a text payload to be sent to the defined topic. That way I can avoid having to re-write the same mqtt.publish config everywhere.

https://www.home-assistant.io/components/notify.mqtt/

A little more background: I was using Telegram as my main notification service, but somewhere something broke, I keep getting error code 502’s for that notifier. I thought it was due to the api key, but after adding a new api key … same issue. However, I have an mqtt service running that pushes everything to telegram that lands on a specific topic, So I’d like to make use of that one as my main notification service

There is not configuration needed.

The provided sample in the documentation works fine. It would help if you post your “Service data”.

Ah, i totally missed that part. I honestly thought I needed to add a config in the notifiers part of my config, which could be used everywhere.
I now understand I need to add the service call everywhere i’d like to use an mqtt “notifier”. not 100% what I wanted but it’ll do :slight_smile:
Thanks for helping me see what I missed :slight_smile:

You could roll your own using a script, and call that script everywhere you would use notify, in exactly the same way as you would use notify, which would save you a lot of repetition.

Something like…

automation:
  alias: Send me a message when I get home 
  trigger:
    platform: state 
    entity_id: device_tracker.me 
    to: 'home' 
  action:
    service: script.notify_mqtt
    data:
      target: "me" 
      message: "I'm home" 


script:
  notify_mqtt:
    sequence:
      - service: mqtt.publish
        data_template:
          payload: "{{ message }}"
          topic: home/"{{ target }}"
          retain: true
3 Likes

Sounds perfect! I’ll give this a try tonight

1 Like

I added the example from @anon43302295 to the documentation.

2 Likes

I didn’t have a lot of time to try but I finally got to try it … The config works like a charm (or at least, HA didn’t couch up errors :slight_smile: ).
But my issue remains due to the fact that I’d like to use this in an Alert. Alerts only take notifiers as params, and mqtt notifiers being the odd-one of the bunch doesn’t seem to work on my end.

Any chance you have a working example of using the mqtt notification component in an alert? The alert I have configured is to fire a message when my garage port stays open for 5 mins.
If i can’t use this directly i’ll setup a REST api to forward to mqtt, but I’m sure you can see this isn’t optimal :slight_smile:

The alert component is basically an automation, then the looped notifications can be handled by a script. TTS can’t be used for Alerts for the same reason, but here’s how…

(note the fix a couple of posts below)

thx for the update. no mqtt notifications for alerts :frowning: i’ll use a workaround then. once I find time to implement it i’ll add it here for future reference.

Got it working with a workaround of running a rest API to forward the message to the mqtt broker.
A simplified version of the code can be found below (requires flask & paho.mqtt)

from flask import Flask, request
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

app = Flask(__name__)

@app.route("/mqtt/", methods=['GET'])
def mqttPub():
    message = request.args.get('message', '')
    topic = "<TOPIC>"
    host = "<HOST>"
    clientId = "<ClientId>"
    username = "<USER>"
    password = "<PASS>"
    port = xxxx
    publish.single(topic, message, hostname=host, retain=True, port=port, client_id=clientId, keepalive=60, auth={'username':username , 'password':password})
    return "OK"


if __name__ == '__main__':
    app.run(debug=True)