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.
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
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
Thanks for helping me see what I missed
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
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 ).
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
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…
thx for the update. no mqtt notifications for alerts 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)