Variablepart of start stop a service with templating

I try to make a variable executing of a service based on a voice command part.
i checked the conversion with two notify commands and my command is translaten from
when i say: zet avond op aan
slotwword on is translated to on
But my input.boolean.turn_on, does not turn on!
Anyone a suggestion?

  - platform: conversation
    command:
      - Zet avond [op] {actie}
    id: avond_spraak
    enabled: true

      - conditions:
          - condition: trigger
            id:
              - avond_spraak
        sequence:
          - service: notify.persistent_notification
            data:
              title: Debug1
              message: debug1:{{trigger.slots.actie}}
            enabled: true
          - service: notify.persistent_notification
            data:
              title: Debug1
              message: >-
                {% if trigger.slots.actie == 'aan' %} on {% else %} off {% endif
                %}
            enabled: true
          - service: >-
              input_boolean.turn_"{% if trigger.slots.actie == 'aan' %}on{% else
              %}off{% endif %}"
            target:
              entity_id: input_boolean.scene_avond
            enabled: true

The problem is due to the presence of the double-quotes. They are being included in the final result thereby producing an invalid service call (check the Log, there should be an error message).

          - service: >-
              input_boolean.turn_"{% if trigger.slots.actie == 'aan' %}on{% else
              %}off{% endif %}"

The template is on a separate line from the service: option and you are using the >- line-continuation characters so there’s no need to wrap the template in quotes. Remove the double-quotes.

Here’s a version that uses a variable with an Immediate If.

          - variables:
              mode: "{{ iif(trigger.slots.actie == 'aan', 'on', 'off') }}"
          - service: notify.persistent_notification
            data:
              title: Debug1
              message: "{{ mode }}"
            enabled: true
          - service: "input_boolean.turn_{{ mode }}"
            target:
              entity_id: input_boolean.scene_avond
            enabled: true

Learned again!
And the solution with a variable is also much nicer!
Thanks Taras!

1 Like

Hi Taras,
normally you see a {% percentages sign with templates in documentation, why dont i have to use it here?

From Jinja’s documentation:

Ok, and can you use statements and comments in for example an automation?
Then i am done with keepon questioning :slight_smile:
Newbie with HASS, and eager to make great scripts. just used your variable solution in my action, and much more readable!

  - conditions:
      - condition: trigger
        id:
          - avond_spraak
    sequence:

{# this is documentation #}
- variables:
xmode: “{{ iif(trigger.slots.actie == ‘aan’, ‘on’, ‘off’) }}”
- service: input_boolean.turn_{{xmode}}
target:
entity_id: input_boolean.scene_avond
enabled: true