Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot output json from a service call when the service accepts templates #55455

Closed
koying opened this issue Aug 30, 2021 · 4 comments
Closed

Comments

@koying
Copy link
Contributor

koying commented Aug 30, 2021

The problem

Ref: https://community.home-assistant.io/t/file-output-with-curly-braces/333609?u=koying

It's not possible to output valid JSON from a template in a service call.

Outputting a non-template JSON string works.
Outputting anything that cannot be inferred as a dict (or a list) by literal_eval also works

After debugging, it appears to me that service calls are "pre-rendering" templates with parse_result at default "True".
For JSON (or anything that looks like a list or a dict), the result will the be inferred as as a list or dict.

Specifically for a notify call with a json message, the input received from the service call is a homeassistant.helpers.template.gen_result_wrapper.<locals>.Wrapper, which actually wraps a dict.
The template validation then fails at https://github.com/home-assistant/core/blob/dev/homeassistant/helpers/config_validation.py#L546

The actual rendering of notify, with parse_result=False is never reached.

What is version of Home Assistant Core has the issue?

dev

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

No response

Link to integration documentation on our website

No response

Example YAML snippet

Not working:

service: notify.file_notif
data_template:
  message: >
    {"key": "{{states('sun.sun') }}" }
  title: Your Garage Door Friend
  target: platform specific
  data: {}
2021-08-30 12:31:02 ERROR (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Error executing script. Invalid data for call_service at pos 1: template value should be a string for dictionary value @ data['message']
2021-08-30 12:31:02 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140265971691728] Error handling message: template value should be a string for dictionary value @ data['message']. Got None

Working

service: notify.file_notif
data_template:
  message: >
    {"key": "bla" }
  title: Your Garage Door Friend
  target: platform specific
  data: {}

Anything in the logs that might be useful for us?

No response

Additional information

Working workaround:

--- a/homeassistant/helpers/config_validation.py
+++ b/homeassistant/helpers/config_validation.py
@@ -543,7 +543,7 @@ def template(value: Any | None) -> template_helper.Template:
     """Validate a jinja2 template."""
     if value is None:
         raise vol.Invalid("template value is None")
-    if isinstance(value, (list, dict, template_helper.Template)):
+    if isinstance(value, (template_helper.Template)):
         raise vol.Invalid("template value should be a string")
 
@koying koying changed the title Cannot output json from a service call Cannot output json from a service call when the service accepts templates Aug 30, 2021
@probot-home-assistant
Copy link

Hey there @PhracturedBlue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)


template documentation
template source
(message by IssueLinks)

@balloob
Copy link
Member

balloob commented Sep 4, 2021

Use config validator cv.string as it will revert the list/dict back to it's original string version before it was parsed.

@balloob balloob closed this as completed Sep 4, 2021
@koying
Copy link
Contributor Author

koying commented Sep 5, 2021

@balloob Sorry, I don't understand your comment in relation to the closure
Do you mean it's fixed, invalid, or a note to do some action (and if so to whom)?

Thanks

@koying
Copy link
Contributor Author

koying commented Sep 7, 2021

EDIT:
Nevermind. I think get your point that cv.string should be used in service schemas, and never cv.template, so in this instance the issue lies in the notify integration...

@github-actions github-actions bot locked and limited conversation to collaborators Oct 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants