How to get the Service Call variable in Developer tools template?

With the service calls calendar.list_events and recent weather.get_forecast, there seems to be a new trend going on. Call a service, dump the output in a variable and in the next automation action you can play with it.

Now I am wondering what’s the best approach to get this output variable in Developer tools → Template.

The reason I am asking is that I want to test my template actions based on representative data and currently it feels like the new trend creates a challenge in doing so. My current approach is to have a test script that does the service call, try to write the template in the next action and dump the output to a persistent notification to see if the output matches my expectation.

That’s more trial and error than I prefer. So I am wondering what your (suggested) approach on this is?

There’s no direct way to access the output since it’s not in the state machine.

It’s a little cumbersome, but if you need a way to test response outputs in the Template editor, try the following:

By calling the service in the Developer tools > Service tool you can get a YAML representation of the response data. Paste that into a YAML to json converter. In the template editor, set a variable equal to the output of the converter. Use that to design your templates.

UPDATE 2024.08

The Action tool (formerly Services tool) has been updated with an additional button, “Copy to Clipboard (Template)”, which will copy the response as a Jinja set statement using action_response as the variable name like:

{% set action_response = {{"weather.home":{"forecast":[{"datetime":"2024-08-23T11:00:00+00:00","cloud_coverage":8, ....] }} %}

This can be pasted directly into the Template Editor tool and used as is. Once pasted, you may want to change the variable name from action_response to whatever you plan to use as your response variable name in your automation or sensor.

2 Likes

That sounds doable, thanks.

AUTOMATION SUGGESTION:

The automation user interface (UI) does not seem to support the new change with service call in the action portion of the automation once the response variable is declared else then through yaml entry with the template UI.

Would it be possible for users without yaml knowledge to do as with the new trigger ID condition and offer a choice of available response.parameter to use next in the condition. So the template format is transparent to users wihtout the need to address it via yaml.

Thanks for reading suggestion.

If you consider this suggestion as a feature request, I’d suggest you post it in the feature request section.

You can use Copy to clipboard (template) button in Service (Action) menu. This will create a variable with assigned value from a service call. Then paste it to the Template page and use as you wish.

1 Like