Home Assistant: Send SMS message when MQTT topic changes to specific value

Good Morning: I’m new to Home assistant and looking for some guidance.
I have an application running “AqualinkD” for home pool automation. Fantastic product for those using Jandy and want to automate the process - or set up Google Home or Alexa or HomePod and talk with it - but I digress…

I am looking to have home assistant send me a text message when a topic changes to a specific value so that I know to fix it.

The topic is: aqualinkd/SWG/fullstatus
These are the values: (0 on, 1 no flow, 2 low salt, 4 high salt, 8 clean cell, 9 turning off, 16 high current, 32 low volts, 64 low temp, 128 check PCB, 255 off)

Ultimately I’d like to have either an e-mail or text message send to my phone so that I know if there is a challenge. For example would love to know when to clean cell (8), or if salt value is too high (4) or low (2), and so forth and it to send me a message coordinating to the error.

I was researching if mqtt can send messages (SMS or text) but didn’t get a firm answer as I saw mqtt.publish as well as some older topics saying to use a pushbullet plugin which I’d rather not install another module but will if need be.

I added this to automations.yaml but think its not right:

automation:

  • alias: Clean Salt Water Cell
    trigger:
    platform: mqtt
    topic: aqualinkd/SWG/fullstatus
    payload: ‘8’
    action:
    service: mqtt.publish
    data:
    topic: aqualinkd/SWG/fullstatus
    payload: “Clean Salt Cell”

Any guidance or recommendations? I am running 0.97.1 and have mqtt mosquitto installed.

please read this post, specifically point 11 to learn how to properly format your code.

Do a search if your mobile provider supports e-mail to SMS. I know sprint in the US does and can be setup to send a text message using the SMTP notification integration in HA.

Below is a link to all the notification integrations, all can be used to send a message from HA. Find the one that works for you.

You are thinking incorrectly about this. Your current automation changes the mqtt topic from a number to some text. This is pointless.

What you need to do is get a sensor into home assistant. Use an MQTT sensor with a template, the template can have logic to create text from the number.

Then set up an automation triggered by a change in that sensor to send a notification by whatever of the notification integrations you wish to use (as per @silvrr’s post).

I use the iOS HA app and use it to send a message (not an SMS) from Home Assistant when a MQTT sensor changes to a certain value… So my coffee roaster publishes a message to my MQTT broker and HA has a MQTT sensor and what that state changes I get a message… You could also use HTML5 if you use Android in the same way (I think)

Thanks @silvrr for the guidance, I was able to use the SMTP to help with the initial email and text messaging. I added this to my configuration.yaml.


  - platform: smtp
    name: aqualinkdswg
    server: smtp.gmail.com
    port: 587
    timeout: 15
    sender: [email protected]
    encryption: starttls
    username: [email protected]
    password: 2_factor_google_pw
    recipient:
      - [email protected]
      - [email protected]
    sender_name: AqualinkD

I then added this to automations.yaml

  trigger:
    platform: mqtt
    topic: aqualinkd/SWG/fullstatus
    payload: "8"
  action:
    - service: notify.aqualinkdswg
      data:
        title: 'AqualinkD SWG'
        message: 'Salt Water Cell needs cleaning'

- alias: 'Saltwater Cell Message - Low Salt'
  trigger:
    platform: mqtt
    topic: aqualinkd/SWG/fullstatus
    payload: "2"
  action:
    - service: notify.aqualinkdswg
      data:
        title: 'AqualinkD SWG'
        message: 'Salt Water Cell - add Salt to pool'

- alias: 'Saltwater Cell Message - High Salt'
  trigger:
    platform: mqtt
    topic: aqualinkd/SWG/fullstatus
    payload: "4"
  action:
    - service: notify.aqualinkdswg
      data:
        title: 'AqualinkD SWG'
        message: 'Salt Water Cell - too much Salt in pool - need to drain'

- alias: 'Saltwater Cell Message - High Current'
  trigger:
    platform: mqtt
    topic: aqualinkd/SWG/fullstatus
    payload: "16"
  action:
    - service: notify.aqualinkdswg
      data:
        title: 'AqualinkD SWG'
        message: 'Salt Water Cell - High Current - check Aquapure'

- alias: 'Saltwater Cell Message - Low Current'
  trigger:
    platform: mqtt
    topic: aqualinkd/SWG/fullstatus
    payload: "32"
  action:
    - service: notify.aqualinkdswg
      data:
        title: 'AqualinkD SWG'
        message: 'Salt Water Cell - Low Voltage - Likely need to clean cell'

I’d love to find a better way to send messages, like only send a message when status changes to one of those payloads once a day, as well as possibly consolidate the way the trigger and actions are written (I have 8 different ones, but consolidated it down for this exercise)

There may be a cleaner way to do it but this approach works for me.

within your action section, turn off the automation after it triggers once.

    action:
      - service: automation.turn_off
        entity_id: automation.namegoeshere

Then reset it each morning in the a.m.

 - alias: "Reset  Notifications"
    initial_state: true
    trigger:
      platform: time
      at: '01:00'
    action:
      - service: automation.turn_on
        entity_id: automation.namegoeshere

You can see a full implementation of this in my feed the dog automations, works well (I am turning on a light rather then sending a e-mail but you get the idea)
https://github.com/SilvrrGIT/HomeAssistant/blob/e0f9cbe5692b6eb2149e009c2b61772a2f11169c/automation/feed_the_dog.yaml

You can definitely add multiple triggers. Just make a list of the triggers as shown below.

  trigger:
    - platform: mqtt
      topic: aqualinkd/SWG/fullstatus
      payload: "2"
    - platform: mqtt
      topic: aqualinkd/SWG/fullstatus
      payload: "3"
    - platform: mqtt
      topic: aqualinkd/SWG/fullstatus
      payload: "4"

The tricky part is changing the notification message based on the mqtt message that triggered the automation. This involves some templating which I have not done for an automation like this. Maybe @petro can help.

This should be an all in 1 for you. It should only trigger when the payload is equal to 2, 4, 8, 16, and 32.

- alias: 'Saltwater Cell Message'
  trigger:
    platform: mqtt
    topic: aqualinkd/SWG/fullstatus
  condition:
    condition: template
    value_template: >
       {{ trigger.payload | int in [ 2, 4, 8, 16, 32 ] }}
  action:
    - service: notify.aqualinkdswg
      data_template:
        title: 'AqualinkD SWG'
        message: >
          {% set payloads = {
               2:' - add Salt to pool',
               4:' - too much Salt in pool - need to drain',
               8:' needs cleaning',
               16:' - High Current - check Aquapure',
               32:' - Low Voltage - Likely need to clean cell' } %}
          Salt Water Cell{{ payloads[trigger.payload | int] }}
1 Like

Thanks @petro, I like the way this looks.

I’m receiving an error when I do this:

2019-08-21 11:31:32 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ':', got 'string') for dictionary value @ data['action'][0]['data_template']['message']. Got "{% set payloads = {\n     0:' - turned On',\n     2:' - add Salt to Pool',\n     4:' - too much Salt in Pool - neet do drain',\n     8:' - cell needs cleaning',\n     16 ' - High Current - check Aquapure',\n     32:' - Low Voltage - Likely need to clean cell',\n     64:' - Low Temp, not producing chlorine',\n     128:' - Check PCB' } %}\nSalt Water Cell{{ payloads[trigger.payload | int] }}\n". (See /home/homeassistant/.homeassistant/automations.yaml, line 20). Please check the docs at https://home-assistant.io/components/automation/ 

Here’s my automations code:

- alias: 'Saltwater Cell Message'
  trigger:
    platform: mqtt
    topic: aqualinkd/SWG/fullstatus
  condition:
    condition: template
    value_template: >
       {{ trigger.payload | int in [ 0, 2, 4, 8, 16, 32, 64, 128 ] }}
  action:
    - service: automation.turn_off
      entity_id: automation.aqualinkdswg
    - service: notify.aqualinkdswg
      data_template:
        title: 'AqualinkD SWG'
        message: >
          {% set payloads = {
               0:' - turned On',
               2:' - add Salt to Pool',
               4:' - too much Salt in Pool - neet do drain',
               8:' - cell needs cleaning',
               16 ' - High Current - check Aquapure',
               32:' - Low Voltage - Likely need to clean cell',
               64:' - Low Temp, not producing chlorine',
               128:' - Check PCB' } %}
          Salt Water Cell{{ payloads[trigger.payload | int] }}

I’ve played with this a few times and initially had some syntex errors I found, but am stuck here.

Note this is on a raspberry pi running Raspbian lite latest version as of last night

Second note, @silvrr I think I got what you are saying. Added the above code plus:

- alias: "Reset  Notifications"
  initial_state: true
  trigger:
    platform: time
    at: '01:00'
  action:
    - service: automation.turn_on
      entity_id: automation.aqualinkdswg

Because you forgot a colon on the line with 16

               16 ' - High Current - check Aquapure',
                 ^
                 |
            NO COLON
               16:' - High Current - check Aquapure',

Note that you will get only one notification per day this way. If sends a ‘add salt to pool’ in the morning you wont get another message even if it sends other codes (ie. check PCB) later in the day.

Also, I think there is an extra space in “Reset Notifications”

@silvrr & @petro,
Thank you for your support, those tricks fixed it!

Is there a good place which can check code? IE my error with 16 vs 16:. Simple error but I spent to long looking for it ;-/ . I’m new to programming (well I did it about 25 years ago).

Template editor in the dev tools menu item. Last tab. Paste your templates there and you can play around until errors go away.