Service_template

Hello! I’m trying to set automation to get parameter from MQTT broker and set the input boolean state. Here is my code:

  - alias: Test
    initial_state: true
    trigger:
      platform: mqtt
      topic: matrix/WiFiPanel-0/dta
    action:
      - service_template: >
          {% if {{trigger.payload.split(" ")[1].split(";")[0].split("|")[0].split(":")[1]}} == 0 %}
          input_boolean.turn_off
          {% else %}
          input_boolean.turn_on
          {% endif %}
        data:
          entity_id: input_boolean.switch_state

But it doesn’t work. I tried to modify it in different ways, but no luck. I’m getting this error in log:

Invalid config for [automation]: Service input_boolean.turn_"{{ 'off' if {% if {{trigger.payload.split(" ")[1].split(";")[0].split("|")[0].split(":")[1]}} == 0 else 'on' }}" does not match format <domain>.<name> for dictionary value @ data['action'][2]['service_template']. Got None. (See /config/configuration.yaml, line 56).

What I’m doing wrong?

The entire line is a template, don’t place template indicators inside each other. e.g. {% %} indicate a template line that does not output anything. {{ }} indicates a line that outputs something. lines without {% %} or {{ }} output the raw string.

          {% if trigger.payload.split(" ")[1].split(";")[0].split("|")[0].split(":")[1] == 0 %}
          input_boolean.turn_off
          {% else %}
          input_boolean.turn_on
          {% endif %}

EDIT: Also, welcome to the community!

1 Like

Thank you very much! I struggled with this for 5 hours, and it was so easy!