You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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_notifdata_template:
message: > {"key": "{{states('sun.sun') }}" }title: Your Garage Door Friendtarget: platform specificdata: {}
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_notifdata_template:
message: > {"key": "bla" }title: Your Garage Door Friendtarget: platform specificdata: {}
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")
The text was updated successfully, but these errors were encountered:
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
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)
@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)?
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...
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 worksAfter 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:
Working
Anything in the logs that might be useful for us?
No response
Additional information
Working workaround:
The text was updated successfully, but these errors were encountered: