In Alert call script via notify

I have a script called “skriptsprachausgabe” which I am calling for voice output on my Echos devices. I have created a notify REST Interface to invoke that script. In the Developer Tools under Service I can successfully call the Rest Service and thus the script, but when I try to include the Notify Service in the alert I get errors and it is not clear to me how to do this. Can someone help me here?

Notify in configuration.yaml

notify:
  - name: skriptsprachausgabe
    platform: rest
    resource: "http://localhost:8123/api/services/script/1698594600049"
    method: POST_JSON
    headers:
      authorization: !secret restapi_notify
      content-type: "application/json"
    message_param_name: message
    title_param_name: title
    data:
      type: "{{ data.type }}"
      duration: "{{ data.duration }}"

Alert

whirlpool_temperatur:
  name: Die Temperatur des Whirlpools ist unter 6 Grad
  entity_id: binary_sensor.whirlpool_temperatur_alarm
  state: "on"
  skip_first: true
  repeat:
    - 30
    - 45
    - 60
  notifiers:
    #- alexa_media_alle_lautsprecher
    - skriptsprachausgabe
      data:
        message: "Achtung: Die Temperatur des Whirlpools ist unter 6 Grad"
        title: "Temperatur des Whirlpools"
        data:
          type: tts
          duration: 6.3

Just a thought (I have no experience with extended notifiers): According to the documentation, the data field is first level (means: has no indentation).

Not sure how this should look like:

image
But that didn’t help

No, I mean

…
notifiers:
  - …
data:
  …
  …

I have now changed it so that Data is at the top level and now I have no more errors. However, it does not work, no message is output on my echoes. If the Data object is at this level, doesn’t it also refer to notififiers? It should only be set for " - skriptsprachausgabe". Does anyone else have an idea that could help me?

image

Could you please share the config text pasted into the forum (correctly formatted) rather than pasting images of text. We cant copy and edit your images as easily as text.

See the example here for the format you should be using: https://www.home-assistant.io/integrations/alert/#message-templates

The message key goes at the top level alongside the data key.

(this would have been a lot easier to show if it was text):
a0188c6932a114cc82bbb29ee44f2f9448ae76e7

1 Like

In the first post above I have inserted the code accordingly as text, in the answers later I wanted to show the error messages with the pictures, sorry.
I have now changed the alert accordingly. However, I still don’t hear any messages on my echoes but I see errors in the log. It looks like the script is called but the parameters are not handed over correctly. What am I doing wrong?

Alert:

whirlpool_temperatur:
  name: Die Temperatur des Whirlpools ist unter 6 Grad
  entity_id: binary_sensor.whirlpool_temperatur_alarm
  state: "on"
  skip_first: true
  repeat:
    - 30
    - 45
    - 60
  notifiers:
    #- alexa_media_alle_lautsprecher
    - skriptsprachausgabe
  message: "Achtung: Die Temperatur des Whirlpools ist unter 6 Grad"
  data:
    title: "Temperatur des Whirlpools"
    data:
      type: "tts"
      duration: 6.3
    #- alexa_media_wohnzimmersonosone2

Log entry:

2024-01-24 13:21:56.517 ERROR (MainThread) [homeassistant.components.script.1698577146089] Alexa say: Error rendering variables: TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'
2024-01-24 13:21:56.517 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 567, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2305, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "
TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 594, in _async_run
    return await self.script.async_run(script_vars, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1535, in async_run
    variables = self.variables.async_render(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script_variables.py", line 62, in async_render
    rendered_variables[key] = template.render_complex(
                              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 237, in render_complex
    return value.async_render(variables, limited=limited, parse_result=parse_result)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 569, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

Since you haven’t shared your script, I don’t know if this approach will work for you, but this is how I use alerts with scripted logic. Instead of using a REST notifier, I use a notify group with an empty set as the value of services. Alerts can be targeted at that notifier, and an automation can be triggered off the service call event created by the alert.

notify:
  - platform: group
    name: scripted_alerts
    services: []

alert:
  whirlpool_temperatur:
    name: Die Temperatur des Whirlpools ist unter 6 Grad
    entity_id: input_boolean.ztest_new_bool
    state: "on"
    skip_first: true
    repeat:
      - 1
      - 2
      - 3
    notifiers:
      - scripted_alerts
    message: "Achtung: Die Temperatur des Whirlpools ist unter 6 Grad"
    title: "Temperatur des Whirlpools"
    data:  
      type: "tts"
      duration: 6.3

automation:
  - id: scripted_alerts_00001
    alias: Scripted Alerts
    description: Play message from "scripted_alerts"
    trigger:
      - platform: event
        event_type: call_service
        event_data:
          domain: notify
          service: scripted_alerts
        variables:
          data: "{{trigger.event.data.service_data.data | to_json}}"
    condition: []
    action:
      - service: script.sprachausgabe
        data:
          message: "{{trigger.event.data.service_data.message}}"
          title: "{{trigger.event.data.service_data.title}}"
          type: "{{data.type}}"
          duration: "{{data.type}}"

EDIT: Modified action to reflect OP’s script call.

That looks very interesting. I can then call my script in the action part instead of directly calling the echo. Cool, I’ll try it out!

It worked and I got the message on at least one Echo. However, I still have an error in the log. Not sure what this mean and how to solve.

Log:

2024-01-25 10:59:25.284 ERROR (MainThread) [homeassistant.components.script.1698577146089] Alexa say: Error rendering variables: TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'
2024-01-25 10:59:25.284 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 567, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2305, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "
TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 594, in _async_run
    return await self.script.async_run(script_vars, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1535, in async_run
    variables = self.variables.async_render(
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script_variables.py", line 62, in async_render
    rendered_variables[key] = template.render_complex(
                              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 237, in render_complex
    return value.async_render(variables, limited=limited, parse_result=parse_result)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 569, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

Alert:

whirlpool_temperatur:
  name: Die Temperatur des Whirlpools ist unter 6 Grad
  entity_id: binary_sensor.whirlpool_temperatur_alarm
  state: "on"
  skip_first: true
  repeat:
    - 30
    - 45
    - 60
  notifiers:
    #- alexa_media_alle_lautsprecher
    - scripted_alerts
  message: "Achtung: Die Temperatur des Whirlpools ist unter 6 Grad"
  title: "Temperatur des Whirlpools"
  data:
    type: "tts"
    duration: 6.3

automation:

alias: Nachrichten aus Alarmen
description: Gib die Nachricht aus Alarmen auf verschiedenen Echos wieder
trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: notify
      service: scripted_alerts
    variables:
      data: "{{trigger.event.data.service_data.data | to_json}}"
condition: []
action:
  - service: script.1698594600049
    data:
      message: "{{trigger.event.data.service_data.message}}"
      title: "{{trigger.event.data.service_data.title}}"
      type: "{{data.type}}"
      duration: "{{data.duration}}"
mode: parallel

Trigger Script (1698594600049):

alias: Sprachausgabe mehrere Alexas
description: Say something with a Group of Alexa devices (tts or announce)
fields:
  titel:
    description: der Title der Nachricht
    example: Fenster/Tür offen
    required: true
  message:
    description: the message (SSML)
    example: <amazon:effect name="whispered">Please close the door.</amazon:effect>
    required: true
  type:
    description: tts or announce
    example: tts
    required: true
  duration:
    description: the length of the speach in seconds
    example: 3.3
    required: true
sequence:
  - if:
      - condition: not
        conditions:
          - condition: state
            entity_id: media_player.wohnzimmersonosone2
            state: unavailable
    then:
      - service: script.turn_on
        target:
          entity_id: script.1698577146089
        data:
          variables:
            message: "{{ message }}"
            type: "{{ type }}"
            target: media_player.wohnzimmer
            target2: media_player.wohnzimmersonosone2
            duration: "{{ duration }}"
        alias: Ausgabe Sonos Wohnzimmer
    alias: " Ausgabe Sonos Wohnzimmer"
  - service: script.turn_on
    target:
      entity_id: script.1698577146089
    data:
      variables:
        message: "{{ message }}"
        type: "{{ type }}"
        target: media_player.wohnzimmer
        target2: media_player.wohnzimmersonosone2
        duration: "{{ duration }}"
    alias: Ausgabe Sonos Wohnzimmer
    enabled: false
  - if:
      - condition: not
        conditions:
          - condition: state
            entity_id: media_player.kuche
            state: unavailable
    then:
      - service: script.turn_on
        target:
          entity_id: script.1698577146089
        data:
          variables:
            message: "{{ message }}"
            type: "{{ type }}"
            target: media_player.kuche
            target2: media_player.kuche
            duration: "{{ duration }}"
    alias: Ausgabe Alexa Küche
  - service: script.turn_on
    target:
      entity_id: script.1698577146089
    data:
      variables:
        message: "{{ message }}"
        type: "{{ type }}"
        target: media_player.schlafzimmer
        target2: media_player.schlafzimmer
        duration: "{{ duration }}"
    alias: Ausgabe Alexa Schlafzimmer
  - if:
      - condition: not
        conditions:
          - condition: state
            entity_id: media_player.bad
            state: unavailable
    then:
      - service: script.turn_on
        target:
          entity_id: script.1698577146089
        data:
          variables:
            message: "{{ message }}"
            type: "{{ type }}"
            target: media_player.bad
            target2: media_player.bad
            duration: "{{ duration }}"
    alias: Ausgabe Alexa Bad oben
  - alias: Ausgabe auf Alexa Sydney
    if:
      - condition: not
        conditions:
          - condition: state
            entity_id: input_select.sydney
            state: Schlafend
        alias: Aber nur wenn Sydney nicht schläft
      - condition: not
        conditions:
          - condition: state
            entity_id: media_player.sydney
            state: unavailable
        alias: Nur wenn der Echo von Sydney auch online ist
    then:
      - service: script.turn_on
        target:
          entity_id: script.1698577146089
        data:
          variables:
            message: "{{ message }}"
            type: "{{ type }}"
            target: media_player.sydney
            target2: media_player.sydney
            duration: "{{ duration }}"
  - alias: Ausgabe auf Alexa Helen
    if:
      - condition: not
        conditions:
          - condition: state
            entity_id: input_select.helen
            state: Schlafend
        alias: Aber nur wenn Helen nicht schläft
      - condition: not
        conditions:
          - condition: state
            entity_id: media_player.thomas_2_echo
            state: unavailable
        alias: Nur wenn der Echo von Helen auch online ist
    then:
      - service: script.turn_on
        target:
          entity_id: script.1698577146089
        data:
          variables:
            message: "{{ message }}"
          type: "{{ type }}"
          target: media_player.thomas_2_echo
          target2: media_player.thomas_2_echo
          duration: "{{ duration }}"
  - alias: Ausgabe auf Alexa Lennard
    if:
      - condition: not
        conditions:
          - condition: state
            entity_id: input_select.lennard
            state: Schlafend
        alias: Aber nur wenn Lennard nicht schläft
      - condition: not
        conditions:
          - condition: state
            entity_id: media_player.lennard
            state: unavailable
        alias: Aber nur wenn der Echo von Lennard auch online ist
    then:
      - service: script.turn_on
        target:
          entity_id: script.1698577146089
        data:
          variables:
            message: "{{ message }}"
          type: "{{ type }}"
          target: media_player.lennard
          target2: media_player.lennard
          duration: "{{ duration }}"
  - if:
      - condition: not
        conditions:
          - condition: state
            entity_id: media_player.terrasse
            state: unavailable
    then:
      - service: script.turn_on
        target:
          entity_id: script.1698577146089
        data:
          variables:
            message: "{{ message }}"
            type: "{{ type }}"
            target: media_player.terrasse
            target2: media_player.terrasse
            duration: "{{ duration }}"
    alias: Ausgabe Alexa Terrasse
mode: parallel
icon: mdi:message-fast

Echo script:

alias: Alexa say
description: Say something with an Alexa device (tts or announce)
variables:
  pause: "{{ 3.0 if type == 'tts' else 2.3 | float }}"
  gong: "{{ 1.7 if type == 'announce' else 0.0 | float }}"
  messagevolume: "{{ state_attr(target, 'volume_level') + 0.1 | default(0.6, true) | float }}"
  reset: "{{ state_attr(target, 'volume_level') | default(0.5, true) | float }}"
fields:
  message:
    description: the message (SSML)
    example: <amazon:effect name="whispered">Please close the door.</amazon:effect>
    required: true
  type:
    description: tts or announce
    example: tts
    required: true
  target:
    description: the target devise as a string (Lautstärke)
    example: media_player.wohnzimmer
    required: true
  target2:
    description: >-
      Bei der SONOS ist das Device das die Nachricht ausgibt und das das die
      Lautstärke einstellt unterschiedlich; the target devise as a string
      (Nachricht)
    example: media_player.wohnzimmersonosone2
    required: true
  duration:
    description: the length of the speach in seconds
    example: 3.3
    required: true
sequence:
  - service: notify.alexa_media
    data:
      target: "{{ target2 }}"
      message: >-
        <break time='{{ pause + 0.2 | float }}s'/> {{ message }} <break
        time='1s'/>
      data:
        type: "{{ type }}"
  - delay:
      hours: 0
      minutes: 0
      seconds: "{{ pause | float }}"
      milliseconds: 0
  - service: media_player.volume_set
    data:
      volume_level: "{{ messagevolume }}"
    target:
      entity_id: "{{ target }}"
  - delay:
      hours: 0
      minutes: 0
      seconds: "{{ gong + duration | float }}"
      milliseconds: 0
  - service: media_player.volume_set
    data:
      volume_level: "{{ reset | float }}"
    target:
      entity_id: "{{ target }}"
mode: parallel
icon: mdi:bullhorn

Does nobody have a hint how to find the problem which variable is NULL? In the forum I have read that this often occurs with template sensors but the error is thrown by the echo “Alex” and here I have no template sensor so a variable does not seem to be passed but which one?

Are any of your echoes Gen 1 or 2… I have one Gen2 dot in our Guest room and it doesn’t have the volume_level attribute, so your variable definition for messagevolume would render a Nonetype + float. Add a float(0) to handle the None.

Filters functions take precedence over mathematical operations so make sure to use parentheses for proper distribution:

  messagevolume: "{{ (state_attr(target, 'volume_level')|float(0) + 0.1) | default(0.6, true) }}"
  reset: "{{ state_attr(target, 'volume_level') | default(0.5, true) }}"