Getting errors with the example code from the wiki

I’m trying to learn how to integrate voice assistant into my automation. For that I tried the example from the wiki:

# Example of a templated conversation response resulting in "Testing 123"
- variables:
    my_var: "123"
- set_conversation_response: "{{ 'Testing ' + my_var }}":

But I get this error:

Error in parsing YAML: bad indentation of a mapping entry (line: 4, column: 57)

I can remove the last colon in line 4 and it lets me save the action but when I save the automation I get this:

Message malformed: expected dictionary @ data[‘actions’][0]

Here’s the complete YAML:

alias: Report
description: ""
triggers:
  - trigger: conversation
    command: Report
conditions: []
actions:
  - - variables:
        my_var: "123"
    - set_conversation_response: "{{ 'Testing ' + my_var }}"
mode: single

You have indentation/listing errors.

Declaring variables is an action and setting the conversation response is a second action, you can’t just list them as one item.

http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/

alias: Report
description: ""
triggers:
  - trigger: conversation
    command: Report
conditions: []
actions:
  - variables:
      my_var: "123"
  - set_conversation_response: "{{ 'Testing ' ~ my_var }}"
mode: single
1 Like

Thanks, I got it working now. The issue was I pasted the code in the graphical editor but then it gets that additional - added you only see if you check the full automation code.

This and the example code has an additional colon and after copy paste the indentation gets messed up.

Edit: But now I’m stuck again. This is my current code:

alias: Statusbericht
description: ""
triggers:
  - trigger: conversation
    command:
      - Statusbericht
conditions: []
actions:
  - variables:
      my_var: "{{ states('sensor.server_temp') }}"
  - set_conversation_response: "{{ 'Testing ' + my_var }}"
mode: single

When I call that in the voice assistant I just get „Fertig“? Why?

You are using the non-preferred concatenation operator… and the use of a template is forcing the output to be a float.

Jinja Docs - Other Operators

As I understand it, in the example the value of the variable isn’t being processed through the template engine so it is returned as the string value that was used; but since yours is being rendered by the template engine, it is returned as a floating point number.

When you use + for concatenation, it’s your responsibility to make sure everything is a string.
If you don’t, the template engine will try to mathematically add a string and a float, which isn’t possible.

That’s what happened, so the conversation response failed into it’s default “Done/Fertig” message. You can fix the issue by using either of the following:

  - set_conversation_response: "{{ 'Testing ' + my_var|string }}"
  - set_conversation_response: "{{ 'Testing ' ~ my_var }}"

Thanks, that did it.