Script that calls a script with parameters

Hi all,

I would like to execute a test script via interface (button) that calls another script to use custom sentences for alexa. The prupose is to have one template script used to execute the command and a source script that passes the message to send. I.e. here my test:

test_echo_notify_template:
  alias: Echo Notify Test
  sequence:
    - service: notify.alexa_media
      data:
        target:
          - media_player.echoinput01
        data:
          type: announce
        message: >
          Test from "{{ alexa_message }}"

test_echo_notify:
  alias: Echo Notify Test 2
  sequence:
    - service: script.test_echo_notify_template
      data:
        alexa_message: from a script

Executing ‘test_echo_notify’ the result is alexa that tells me only: “Test from” and so ‘alexa_message’ is not taken into consideration.
In addition I’ve another question as well… how could I debug the scripts ? Is there a way to use logger to debug it?

Thanks!

Simon

The developer @alandtse monitors this thread.

The data supplied to the service notify.alexa_media contains a template {{ alexa_message }} so the script should use data_template.

    - service: notify.alexa_media
      data_template:
        target: media_player.echoinput01
        type: announce
        message: 'Test from {{ alexa_message }}'

Hi @123
Thanks for the suggestion. I’ll give it a try and let you know!
Cheers, Simon

Hello,
It doesn’t work, here the config:

test_echo_notify_template:
  alias: Echo Notify Test
  sequence:
    - service: notify.alexa_media
      data_template:
        target: media_player.echoinputsoggiorno
        type: announce
        message: 'Hello test from {{ alexa_message }}'

test_echo_notify:
  alias: Echo Notify Test 2
  sequence:
    - service: script.test_echo_notify_template
      data:
        alexa_message: a script

and here the error:

2019-06-12 22:34:58 ERROR (MainThread) [homeassistant.components.script] Error executing script script.test_echo_notify_template. Invalid data for call_service at pos 1: extra keys not allowed @ data['type']
2019-06-12 22:34:58 ERROR (MainThread) [homeassistant.components.script] Error executing script script.test_echo_notify. Invalid data for call_service at pos 1: extra keys not allowed @ data['type']
2019-06-12 22:34:58 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall script.test_echo_notify (c:3e9401cccef040018dc7561ff524c2cb)>
Traceback (most recent call last):
  File "/usr/src/app/homeassistant/core.py", line 1159, in _safe_execute
    await self._execute_service(handler, service_call)
  File "/usr/src/app/homeassistant/core.py", line 1172, in _execute_service
    await handler.func(service_call)
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 114, in service_handler
    context=service.context)
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 178, in async_turn_on
    raise err
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 173, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/src/app/homeassistant/helpers/service.py", line 89, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/src/app/homeassistant/core.py", line 1150, in async_call
    self._execute_service(handler, service_call))
  File "/usr/src/app/homeassistant/core.py", line 1172, in _execute_service
    await handler.func(service_call)
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 114, in service_handler
    context=service.context)
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 178, in async_turn_on
    raise err
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 173, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/src/app/homeassistant/helpers/service.py", line 89, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/src/app/homeassistant/core.py", line 1130, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 267, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 589, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.7/site-packages/voluptuous/schema_builder.py", line 427, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: extra keys not allowed @ data['type']

now it doesn’t play anything…

Thanks!

Simon

It doesn’t work because type is not a valid option. That’s what the error message says:

2019-06-12 22:34:58 ERROR (MainThread) [homeassistant.components.script] Error executing script script.test_echo_notify_template. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘type’]
2019-06-12 22:34:58 ERROR (MainThread) [homeassistant.components.script] Error executing script script.test_echo_notify. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘type’]

I had copied your example, assuming everything you had in it was valid. However, according to the error message, type is invalid for use with the service notify.alexa_media. You’ll need to remove it from the script.

test_echo_notify_template:
  alias: Echo Notify Test
  sequence:
    - service: notify.alexa_media
      data_template:
        target: media_player.echoinputsoggiorno
        message: 'Hello test from {{ alexa_message }}'

Hello!
I’ve removed it, anyway, other error :frowning:

2019-06-13 10:20:29 INFO (MainThread) [homeassistant.helpers.script] Script Echo Notify Test 2: Running script
2019-06-13 10:20:29 INFO (MainThread) [homeassistant.helpers.script] Script Echo Notify Test 2: Executing step call service
2019-06-13 10:20:29 INFO (MainThread) [homeassistant.helpers.script] Script Echo Notify Test: Running script
2019-06-13 10:20:29 INFO (MainThread) [homeassistant.helpers.script] Script Echo Notify Test: Executing step call service
2019-06-13 10:20:29 ERROR (MainThread) [homeassistant.components.script] Error executing script script.test_echo_notify_template. Unknown error for call_service at pos 1: 
Traceback (most recent call last):
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 173, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/src/app/homeassistant/helpers/service.py", line 89, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/src/app/homeassistant/core.py", line 1150, in async_call
    self._execute_service(handler, service_call))
  File "/usr/src/app/homeassistant/core.py", line 1172, in _execute_service
    await handler.func(service_call)
  File "/usr/src/app/homeassistant/components/notify/__init__.py", line 112, in async_notify_message
    await notify_service.async_send_message(**kwargs)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/alexa_media/notify.py", line 137, in send_message
    if data['type'] == "tts":
TypeError: 'NoneType' object is not subscriptable
2019-06-13 10:20:29 ERROR (MainThread) [homeassistant.components.script] Error executing script script.test_echo_notify. Unknown error for call_service at pos 1: 
Traceback (most recent call last):
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 173, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/src/app/homeassistant/helpers/service.py", line 89, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/src/app/homeassistant/core.py", line 1150, in async_call
    self._execute_service(handler, service_call))
  File "/usr/src/app/homeassistant/core.py", line 1172, in _execute_service
    await handler.func(service_call)
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 114, in service_handler
    context=service.context)
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 178, in async_turn_on
    raise err
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 173, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/src/app/homeassistant/helpers/service.py", line 89, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/src/app/homeassistant/core.py", line 1150, in async_call
    self._execute_service(handler, service_call))
  File "/usr/src/app/homeassistant/core.py", line 1172, in _execute_service
    await handler.func(service_call)
  File "/usr/src/app/homeassistant/components/notify/__init__.py", line 112, in async_notify_message
    await notify_service.async_send_message(**kwargs)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/alexa_media/notify.py", line 137, in send_message
    if data['type'] == "tts":
TypeError: 'NoneType' object is not subscriptable
2019-06-13 10:20:29 ERROR (MainThread) [homeassistant.core] Error executing service <ServiceCall script.test_echo_notify (c:3647f237a5a54a2e92a0fe621704416e)>
Traceback (most recent call last):
  File "/usr/src/app/homeassistant/core.py", line 1159, in _safe_execute
    await self._execute_service(handler, service_call)
  File "/usr/src/app/homeassistant/core.py", line 1172, in _execute_service
    await handler.func(service_call)
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 114, in service_handler
    context=service.context)
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 178, in async_turn_on
    raise err
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 173, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/src/app/homeassistant/helpers/service.py", line 89, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/src/app/homeassistant/core.py", line 1150, in async_call
    self._execute_service(handler, service_call))
  File "/usr/src/app/homeassistant/core.py", line 1172, in _execute_service
    await handler.func(service_call)
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 114, in service_handler
    context=service.context)
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 178, in async_turn_on
    raise err
  File "/usr/src/app/homeassistant/components/script/__init__.py", line 173, in async_turn_on
    kwargs.get(ATTR_VARIABLES), context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 131, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 210, in _handle_action
    action, variables, context)
  File "/usr/src/app/homeassistant/helpers/script.py", line 299, in _async_call_service
    context=context
  File "/usr/src/app/homeassistant/helpers/service.py", line 89, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/src/app/homeassistant/core.py", line 1150, in async_call
    self._execute_service(handler, service_call))
  File "/usr/src/app/homeassistant/core.py", line 1172, in _execute_service
    await handler.func(service_call)
  File "/usr/src/app/homeassistant/components/notify/__init__.py", line 112, in async_notify_message
    await notify_service.async_send_message(**kwargs)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/alexa_media/notify.py", line 137, in send_message
    if data['type'] == "tts":
TypeError: 'NoneType' object is not subscriptable

Having this setup now:

test_echo_notify_template:
  alias: Echo Notify Test
  sequence:
    - service: notify.alexa_media
      data_template:
        target: media_player.echoinputsoggiorno
#        type: announce
        message: >
          Hello test from {{ alexa_message }}

test_echo_notify:
  alias: Echo Notify Test 2
  sequence:
    - service: script.test_echo_notify_template
      data:
        alexa_message: A Script

Ideas?

Thanks a lot!

Simon

Where is the documentation for the notification component you are using? It will list all the valid options for this component.

It’s a matter of the custom_component?
I’ve used this one: https://github.com/keatontaylor/alexa_media_player/wiki
BTW I’m using it actually only to allow me use alexa as TTS. BTW I could change the component if there is another one which works better :wink:
Thanks, Simon

Here’s this custom component’s documentation for Announce Notifications

Did you read and follow its instructions?

It provides an example of how to enter the options in JSON format:

{
"message":"test",
"title":"My title for Echo show",
"data":{"type":"announce", "method":"all"},
"target":["Guest Room", "media_player.kitchen", "serialNumber"]
}

Here’s the equivalent in YAML format:

message: test
title: My title for Echo show
data:
  type: announce
  method: all
target:
- Guest Room
- media_player.kitchen
- serialNumber

If we apply the documentation’s example to your code, we get this:

test_echo_notify_template:
  alias: Echo Notify Test
  sequence:
    - service: notify.alexa_media
      data_template:
        message: 'Hello test from {{ alexa_message }}'
        title: 'echo notify test'
        data:
          type: announce
          method: all
        target: media_player.echoinputsoggiorno

Hello @123
Thanks a lot for your support, it works!
I’ve to study a little better the language (rtfm), you’re right; have some difficults :slight_smile:
I wish you a nice week-end.
Cheers, Simon

Glad to hear it. Mark my answer as the ‘solution’ so that it will be easier for other people to find (marking it as ‘solution’ will make it appear at the top).

done, thanks again!! :wink:

1 Like