Native Type support for templates - script broken

The new functionality “native type support for templates” has broken a script. I can not find a solution and aks for assistance.
With option “legacy_template” set to true, the script works perfect!

This is my script - it has one parameter “snapshot_url”:

sequence:
  - service: notify.file_service
    data:
      message: |
        {% set file_rec = {
                'timestamp': now().strftime("%Y-%m-%d %H:%M:%S")|string,
                'pic_url': snapshot_url} %}
        {{ file_rec | to_json }}

The script produces following error message:

Logger: homeassistant.components.script.write_filesensor
Source: helpers/script.py:1122
Integration: Skript ([documentation](https://www.home-assistant.io/integrations/script), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+script%22))
First occurred: 15:00:47 (1 occurrences)
Last logged: 15:00:47

Write Filesensor: Error executing script. Invalid data for call_service at pos 1: template value should be a string for dictionary value @ data['message']
Logger: homeassistant
Source: core.py:1405
First occurred: 15:00:47 (1 occurrences)
Last logged: 15:00:47
Error doing job: Task exception was never retrieved

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1010, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 710, in async_run
    await super().async_run()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 245, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 253, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 460, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1405, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: template value should be a string for dictionary value @ data['message']

Has anbody a hint for me?

0.118 inspects the result of a template and, based on its appearance, will assign it a type.

Your template generates a result that look something like this:

{'timestamp': '2020-11-28 12:18:19', 'pic_url': 'http://whatever'}

That looks like a dict and so that’s how 0.118 treats it. Unfortunately, the message option requires a string, not a dict, and that’s why you get an error.

You may wish to open an Issue in the GitHub Core repository and provide this as an example to the development team of a situation where the result should not be treated as a dict.

The only way I know of preventing it from being handled as a dict is to ensure it doesn’t look like one. For example:

<'timestamp': '2020-11-28 12:18:19', 'pic_url': 'http://whatever'>

However, this might not be acceptable for your needs if the goal is to have a filename in JSON format.

1 Like

Thanks for this competent comment!