Using different notification methods based on conditions

I’m trying to implement the ability to send notifications via SMS where my HomeAssistant instance has no access to internet or where I’m without a data connection for my phone.

I currently have working states for when notifications should be via SMS and a working shell_command service to send SMS using gammu. input_boolean.notify_via_sms is set by the system as required and is then used to determine which notification to use.

However I’m struggling with the best method of templating this approach for ease of re-use throughout multiple automations, and hoped that someone here might be able to offer some pointers based on their experience.

My current (testing) scripts are:
SMS notification template - working

# send sms notification using gammu and data_template
sms_notification:
  sequence:
    - condition: state
      entity_id: input_boolean.notify_via_sms
      state: 'on'
    - service: shell_command.send_sms
      data_template: 
        sms_to: "{{ recipient }}" 
        sms_msg: "{{ msg }}"

Pushover notification template - working when called from services

pushover_notification:
  sequence:
    - condition: state
      entity_id: input_boolean.notify_via_sms
      state: 'off'
    - service: notify.pushover
      data_template: 
        title: "{{ title }}"
        message: "{{ msg }}"
        data:
          sound: "{{ sound }}"
          priority: "{{ priority }}"
          timestamp: true

Master notification script - SMS working, pushover not

send_test_notify:
  sequence:
    - service: script.sms_notification
      data_template: 
         recipient: "{{ states.sensor.sms.state }}"
         msg: "{{ message }}"
    - service: script.pushover_notification
      data_template:
        title: "{{ title }}"
        msg: "{{ message }}"
        data: 
          sound: "{{ sound }}"
          priority: "{{ priority }}"

When calling script.send_test_notify with the services tool I get a failure for the pushover message from the master script:

2019-06-23 17:46:17 ERROR (SyncWorker_7) [custom_components.pushover_custom.notify] Could not send pushover notification
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/pushover_custom/notify.py", line 109, in send_message
    self.pushover.send_message(message=message, attachment=file, **data)
  File "/srv/homeassistant/lib/python3.6/site-packages/pushover.py", line 275, in send_message
    return MessageRequest(payload, files)
  File "/srv/homeassistant/lib/python3.6/site-packages/pushover.py", line 129, in __init__
    Request.__init__(self, "post", MESSAGE_URL, payload, files)
  File "/srv/homeassistant/lib/python3.6/site-packages/pushover.py", line 110, in __init__
    raise RequestError(self.answer["errors"])
pushover.RequestError: 
==> message cannot be blank

When calling script.pushover_notification with the same data from services tool the message sends successfully.

So - have I missed something obvious? Is there a better way of achieving my end goal?

I’ve finally figured this out, so rather than pull a DenverCoder9 here’s the working (and much simplified) sample:

# send sms notification using gammu and data_template
send_test_notify:
  sequence:
    - service: script.turn_on
      data:
        variables: 
          title: "This is a test"
          msg: "My test message"
          sound: "bugle"
          priority: 0
          timestamp: true
      data_template:
        entity_id: >
          {% if is_state("input_boolean.notify_via_sms", "off") %}
                script.notify_pushover
          {% else %}
                script.notify_sms
          {% endif %}

notify_sms:
  sequence:
    - service: shell_command.send_sms
      data_template:
        sms_to: "{{ states.sensor.sms.state }}"
        sms_msg: "{{ msg }}"

notify_pushover:
  sequence:
    - service: notify.pushover
      data_template:
        title: "{{ title }}"
        message: "{{ msg }}"
        data:
          sound: "{{ sound }}"
          priority: "{{ priority }}"
          timestamp: true

NB: My original attempt was having issues with quoting on nested variables when calling the command line service for sms, and parameter issues with pushover caused by the nested calls to that service.
states.sensor.sms.state is a template sensor to retrieve the phone number required from secrets.yaml

I still need to add expiry and repeat parameters for the pushover call when priority is 2, but that should be pretty straightforward

1 Like

Hello, im also trying use gammu to send sms messages with some variables from home assistant but no luck. Can You paste Your shell_command.send_sms ?

For now im sending sms messages but without variables from HA and the text and phone number is coded in the file.

Last time when suddenly messages stopped coming i relised that also verry important thing is sendins test messages every time period to check out is sim card working

Heres my automation:

  • alias: “test_gsm_1”
    trigger:
    • platform: time_pattern
      minutes: ‘/1’
      condition:
    • condition: state
      entity_id: input_boolean.gsm_test1_on_off
      state: ‘on’
    • condition: template
      value_template: ‘{{ now().strftime("%H:%M") == states.sensor.gsm_test1_time.state }}’
      action:
    • service: shell_command.gsm_test_1
      data_template:
      var1: ‘{{“this is only a test.” | urlencode}}’

It’s working great, but i cannot pass the values from HA.
I’ll be glad for command_line code

# command line switches
shell_command:
  send_sms: 'gammu sendsms TEXT "{{ sms_to }}" -text "{{ sms_msg }}"'

Note I’m running in a virtual environment on Ubuntu under a dedicated user account, so I tested and configured gammu from within the venv under the user account.

My ISP was kind enough to provide a live test scenario today by knocking out my home connection for an hour while I was at work. My notifications automatically switched to SMS and then reverted when the connection was reinstated.

1 Like

Hello everyone, solution to make gammu working with phone number and message stored in input_text fields

  • alias: “test_gsm_1”
    trigger:
    • platform: time_pattern
      minutes: ‘/1’
      condition:
    • condition: state
      entity_id: input_boolean.gsm_test1_on_off
      state: ‘on’
    • condition: template
      value_template: ‘{{ now().strftime(“%H:%M”) == states.sensor.gsm_test1_time.state }}’
      action:
    • service: shell_command.send_sms
      data_template:
      sms_to: “{{ states.input_text.sms_nr1.state }}”
      sms_msg: “{{ states.input_text.sms_text_nr1.state }}”

send_sms: ‘sudo gammu sendsms TEXT “{{ sms_to }}” -text “{{ sms_msg }}”’

Thanks very much man, i spent months to make it work, You’re Great !!!

1 Like