How to listen to an MQTT topic in automation

I’ve created an automation that triggers a secondary automation to send notification via notify or sms service. The second automation is like a notification service that decides whether to send an SMS or notification based on HA. So any automation that needs to send notification will call this service. I first publish to an MQTT topic (in the first automation) and then want to listen (or use that) in the second notification service but I am unable to call or listen to an MQTT service topic … any idea how to do that ?
… and if that is not possible… what is a better approach of passing data from one automation to another ?

alias: "Notify: Garage door open"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.zone_9
    for:
      hours: 0
      minutes: 5
      seconds: 0
    to: "on"
condition:
  - condition: or
    conditions:
      - condition: time
        before: "08:00:00"
        after: "18:00:00"
        weekday:
          - mon
          - tue
          - wed
          - thu
          - fri
          - sat
          - sun
      - condition: time
        before: "17:00:00"
        after: "09:00:00"
        weekday:
          - fri
          - thu
          - wed
          - tue
          - mon
action:
  - service: mqtt.publish
    data:
      qos: 0
      topic: garage/status
      payload: " Garage left open on {{now().strftime(\"%a, %d-%b-%y at %-I:%M %p\")}}"
      retain: true
  - service: automation.trigger
    data:
      skip_condition: false
    target:
      entity_id: automation.trigger_sms_or_notification
mode: single

second service

alias: "Trigger: SMS or Notification"
description: ""
trigger: []
condition: []
action:
  - if:
      - condition: or
        conditions:
          - condition: time
            before: "08:00:00"
            after: "21:00:00"
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
          - condition: time
            before: "10:00:00"
            after: "21:00:00"
            weekday:
              - sun
              - sat
    then:
      - service: notify.sms
        data:
          message: MQTT topic value in here...'
          target: 0xx
        alias: >-
          Call a service 'Notifications: Send a notification with sms on
          0xx
      - service: notify.sms
        data:
          message: 'MQTT topic value in here...'
          target: 0xxx
        alias: >-
          Call a service 'Notifications: Send a notification with sms on
          0xxx
        enabled: false
    else:
      - service: notify.mobile_app_pixel_pro
        data:
          title: Warning!
          message: MQTT topic value in here...'
      - service: notify.mobile_app_a_iphone
        data:
          message: MQTT topic value in here...'
          title: Warning!
        enabled: false
mode: single

Your trigger should be

platform: mqtt
topic: garage/status

You can add condition for state like

  - condition: template
    value_template: "{{trigger.payload_json["yourvalue"] == thestate }}"

I would need to see message to know what to put in the fake value above

Based on your suggestion I created a JASON payload with state as one of the element and then the message.

action:
  - service: mqtt.publish
    data:
      qos: 0
      topic: garage/status
      retain: true
      payload: >-
        {"state":"Open", "message":" Garage left open on {{now().strftime(\"%a,
        %d-%b-%y at %-I:%M %p\")}}"}

the above payload was not working so had to change it to simple

 payload: >-
        {"state":"Open", "message":" Garage left open on"}

but HA YAML transformed this with additional escape characters:

payload: "{\"state\":\"Open\", \"message\":\" Garage left open on \"}"

and the second automation is like that

alias: "Notify: Test"
description: ""
trigger:
  - platform: mqtt
    topic: garage/status
condition:
  - condition: template
    value_template: "{{trigger.payload_json.state =='Open'}}"
action:
  - service: notify.mobile_app_pixel_8_pro
    data:
      title: Warning!
      message: Test message
    enabled: true
mode: single

Although it worked but how to fix the payload

{"state":"Open", "message":" Garage left open on {{now().strftime(\"%a,
        %d-%b-%y at %-I:%M %p\")}}"}
  • One automation manually triggering another automation is an anti-pattern (i.e. not a best practice).
  • You don’t pass data from one automation to another, you pass it from an automation to a script.
  • An automation without any triggers isn’t an automation, it’s effectively a stunted script.

Simply create a script for sending notifications. Your automation simply calls the script and passes it data via script variables. There’s no need for MQTT or for employing an anti-pattern. You don’t even need to pass the data in JSON format.

Reference

Passing variables to scripts

1 Like

Taras is correct. Wasnt paying attention

Why do you use 2 automations? These two automations should be combined into a single automation. Not sure why you using 2.

EDIT

Pretty sure you can almost take the entire second automation and add it to the first in place of the mqtt.publish action call

Thanks , and I agree to the script option. I m now calling a script

data:
  variables:
    message: " Garage left open on {{now().strftime(\"%a, %d-%b-%y at %-I:%M %p\")}}"
target:
  entity_id: script.test_script

Instead of dummy message, how do I pass the variable from the automation to script in here:

sequence:
  - service: notify.mobile_app_pixel_8_pro
    data:
      title: Warning!
      message: "{{message}}"
    enabled: true
mode: single

Doesn’t your example already pass a variable to a script?

It’s passing the variable message to script.test_script and the script is referencing message’s value by employing it in a template like this {{ message }}.

Sorry, it does, forgot to update my reply. Thanks