Developer Tools - Use variables generated in "Service" to be used in "Template"

I was testing a service call that was going to be used in a template using a variable. I was able to get the service call to work but the response was hard to parse afterward when I was trying to load it into a template. It would be easier to use variables in scripts if you could test in developer tools and save the response in a variable and then access the variable in the template section.

You just need to use

{% set myVariable = "anything like this text" %}
{% set myNumber = 42 %}

in the top then put myVariable, myNumber, or whatever you named it into your test template below scooped right from your code…

You can plug anything into those, HA sensor values, custom templates, helpers, lists, dictionaries, just about anything.

1 Like

There’s a similar workaround proposed in this thread.

I can’t help but feel that this is still very clunky: the extra steps needed, plus the ‘clutter’ of having to preface the template with what can be a quite large chunk of response data. For example, a couple of calendars worth of response data plus weather forecast can get to be quite long…

IMHO the original suggestion would be nice, or at least something that allows for variables to be declared in YAML format separate from the jinja template textarea.

Yeah it was pretty clunky. I was using response data from the todo list which was a dict list object and I wasn’t sure the type formatting of the whole structure so I had to guess and check in automations instead. For just a number to text I understand to be able to copy but with the response of the calendar or todo list it is a little more complicated.

I found the easiest way was to use a dedicated “test” script, that I just update with whatever I need to… test. You can also use “Traces” to get more details on the execution, especially the structure of the variable if it fails to run (select second node > Changed Variables).

alias: Test service call
sequence:
  - service: script.gimmesomethin
    response_variable: result
  - service: notify.persistent_notification
    data:
      message: "{{ result }}"

This is more helpful. I will probably be using this for now thanks

Go to Developer Tools > Services and execute a service call that generates a response_variable. The resulting value will be displayed at the bottom of the screen.

For example, here’s the output for weather.get_forecast.

If you need the value to be converted from YAML to JSON (perhaps for use in the Template Editor for further experimentation) you can use use an online YAML to JSON converter.