Notify household savely with some issues

Hello ha-friends,

via raymondjulin’s blog post i came across the central script to send notifications for our iOS and Alexa devices. Thanks at this point! :slight_smile:

I really liked this and rebuilt it for our needs.
Last step was to include critical notifications for iOS devices when, for example, someone rang the front doorbell (that’s when the bell is off and the Alexa devices whisper).
For this purpose I created the following block:

- service: notify.mobile_app_buddy_iphone_13_pro
            data:
              title: "{{ title }}"
              message: "{{ message }}"
              #
              # everything below here is used for the doorbell automation
              #
              data:
                url: "{{ url }}"
                attachment:
                  image: "{{ image }}"
                  url: "{{ image }}"
                actions:
                  - action: URI
                    title: "{{ actiontitle }}"
                    uri: "{{ uri }}"
                group: "{{ group }}"
                push:
                  sound:
                    name: "{{ name }}"
                    critical: "{{ critical }}"
                    volume: "{{ volume }}"

This works in principle also very well, but the normal notifications for our windows, doors, … no longer work, because some fields are considered mandatory by Home Assistant (at least as I understand the messages in the log).

Logger: homeassistant.helpers.template
Source: helpers/template.py:149
First occurred: 2. März 2023 um 17:38:35 (63 occurrences)
Last logged: 05:49:42

Template variable warning: 'actiontitle' is undefined when rendering '{{ actiontitle }}'
Template variable warning: 'uri' is undefined when rendering '{{ uri }}'
Template variable warning: 'name' is undefined when rendering '{{ name }}'
Template variable warning: 'critical' is undefined when rendering '{{ critical }}'
Template variable warning: 'volume' is undefined when rendering '{{ volume }}'

Notifications only with title and message can be sent via developer tools without any problems.

Question:
How can I keep everything in one script?
Is there any way to set fields as optional?
I could also pass dummy values, but that only works conditionally for the fields.

Here is the whole script:

Summary
# Source: https://www.raymondjulin.com/blog/conditional-notifications-for-the-family-in-home-assistant
notify_everyone_safely:
  alias: Notify household safely
  # We need to run the script in parallel because multiple
  # notifications can come at the same time
  mode: parallel
  # The script should take title and message as inputs
  fields:
    message:
      description: "Message content"
      example: "Washing machine done"
    title:
      description: "Message title"
      example: "Washing machine done"
    url:
      description: "For mobile notification to jump to a view"
      example: "/dashboard-mushroom/fenster"
    group:
      description: "Group specific notifications"
      example: "window-notifications-group"
    image:
      description: "Send a image to your mobile device"
      example: "/local/doorbird/last_ring.jpg"
    actiontitle:
      description: "Add a title to a action on your mobile device"
      example: "open home assistant"
    uri:
      description: "Add a link which should be opened on your mobile device"
      example: "/dashboard-mushroom/sicherheit"
    name:
      description: "Add a special sound the notification on your mobile device"
      example: "RingtoneDucked_UK_Haptic.caf"
    critical:
      description: "Activates critical messages (1 = on, 2 = off)"
      example: "1"
    volume:
      description: "Set the volume for the notification (range 0.0 to 1.0)"
      example: "0.5"
  sequence:
    # Buddy
    - choose:
        # Here we check if the house member should receive a
        # notification at this time
        conditions:
          - condition: state
            entity_id: input_boolean.benachrichtigungen_buddy
            state: "on"
        sequence:
          # Call the notify service of your choice.
          # I use the mobile app notification type
          - service: notify.mobile_app_buddy_iphone_13_pro
            data:
              title: "{{ title }}"
              message: "{{ message }}"
              data:
                url: "{{ url }}"
                attachment:
                  image: "{{ image }}"
                  url: "{{ image }}"
                actions:
                  - action: URI
                    title: "{{ actiontitle }}"
                    uri: "{{ uri }}"
                group: "{{ group }}"
                push:
                  sound:
                    name: "{{ name }}"
                    critical: "{{ critical }}"
                    volume: "{{ volume }}"
          - service: notify.mobile_app_buddy_ipad
            data:
              title: "{{ title }}"
              message: "{{ message }}"
              data:
                url: "{{ url }}"
                attachment:
                  image: "{{ image }}"
                  url: "{{ image }}"
                actions:
                  - action: URI
                    title: "{{ actiontitle }}"
                    uri: "{{ uri }}"
                group: "{{ group }}"
                push:
                  sound:
                    name: "{{ name }}"
                    critical: "{{ critical }}"
                    volume: "{{ volume }}"
    # Katrin
    - choose:
        conditions:
          - condition: state
            entity_id: input_boolean.benachrichtigungen_katrin
            state: "on"
        sequence:
          - service: notify.mobile_app_katrins_iphone_11
            data:
              title: "{{ title }}"
              message: "{{ message }}"
              data:
                url: "{{ url }}"
                attachment:
                  image: "{{ image }}"
                  url: "{{ image }}"
                actions:
                  - action: URI
                    title: "{{ actiontitle }}"
                    uri: "{{ uri }}"
                group: "{{ group }}"
                push:
                  sound:
                    name: "{{ name }}"
                    critical: "{{ critical }}"
                    volume: "{{ volume }}"
    # Alexa
    - choose:
        conditions:
          - condition: state
            entity_id: input_boolean.benachrichtigungen_alexa
            state: "on"
          - condition: state
            entity_id: schedule.nachtmodus
            state: "off"
        sequence:
          # Werkstatt
          - if:
              - condition: or
                conditions:
                  - condition: state
                    entity_id: binary_sensor.buddy_pc
                    state: "on"
                  - condition: state
                    entity_id: binary_sensor.zeiss_laptop
                    state: "off" # change to on after zeiss-laptop got his new ip
            then:
              - if:
                  - condition: state
                    entity_id: input_boolean.ruhemodus
                    state: "on"
                then:
                  - service: notify.alexa_media
                    data:
                      title: "{{ title }}"
                      message: <amazon:effect name='whispered'>{{ message }}</amazon:effect>
                      target: media_player.werkstatt
                      data:
                        type: announce
                        method: speak
                else:
                  - service: notify.alexa_media
                    data:
                      title: "{{ title }}"
                      message: "{{ message }}"
                      target: media_player.werkstatt
                      data:
                        type: announce
                        method: speak
          # Badezimmer
          - if:
              - condition: state
                entity_id: light.badezimmer_licht
                state: "on"
            then:
              - if:
                  - condition: state
                    entity_id: input_boolean.ruhemodus
                    state: "on"
                then:
                  - service: notify.alexa_media
                    data:
                      title: "{{ title }}"
                      message: <amazon:effect name='whispered'>{{ message }}</amazon:effect>
                      target: media_player.badezimmer
                      data:
                        type: announce
                        method: speak
                else:
                  - service: notify.alexa_media
                    data:
                      title: "{{ title }}"
                      message: "{{ message }}"
                      target: media_player.werkstatt
                      data:
                        type: announce
                        method: speak
          # Küche und Wohnzimmer
          - if:
              - condition: state
                entity_id: input_boolean.ruhemodus
                state: "on"
            then:
              - service: notify.alexa_media
                data:
                  title: "{{ title }}"
                  message: <amazon:effect name='whispered'>{{ message }}</amazon:effect>
                  target: media_player.badezimmer
                  data:
                    type: announce
                    method: speak
            else:
              - service: notify.alexa_media
                data:
                  title: "{{ title }}"
                  message: "{{ message }}"
                  target:
                    - media_player.kueche
                    - media_player.wohnzimmer
                  data:
                    type: announce
                    method: speak

Thanks and greetings
Buddinski88

Is there anyone here who can give me help? :slight_smile:

A while ago I created a notify script faxing the same issue and solved it as follows:

# VERWENDUNG:
#
#   - service: script.notify_service
#     data:
#       service: dötchen
#       message: Huhu!


script:

  notify_service:
    alias: notify_service
    description: |-
      zentrale Verwaltung aller 'notify' service calls
      packages/skripte/notify_service.yaml

    variables:
      service: dötchen
      service_source: >-
        {{- service
        |replace('alexa', 'alexa_media')
        |replace('last', 'alexa_media_last_called')
        |replace('dötchen', 'alexa_media_echo_dotchen')
        |replace('show', 'alexa_media_echo_show')
        |replace('google', 'google_nest_mini')
        |replace('persist', 'persistent_notification')
        |replace('push', 'push_benachrichtigungen')
        |replace('sprache', 'sprach_benachrichtigungen') -}}
      target: >-
       {{ 'media_player.echo_' ~ service if service in ['dotchen', 'show']
       else 'group.echos' }}
      title: ''
      message: notify
      type: tts

    sequence:
    - if:
      - "{{ 'alexa' in service_source }}"
      then:
      - service: notify.{{ service_source }}
        data:
          title: '{{ title }}'
          message: '{{ message }}'
          target: '{{ target }}'
          data: 
            type: '{{ type }}'
      else:
      - service: notify.{{ service_source }}
        data:
          title: '{{ title }}'
          message: '{{ message }}'

1 Like

That looks like an interesting solution. Thanks for the tip, I’ll definitely take a look at it :slight_smile: and let you know if I’ve managed to do the same.