Why is my if/else not working in my script?

Hi all,
I found several topics dealing with if/else mostly on automation.
My need : do the same on scripts

my idea is to tell alexa two simple phrases
“put a higher value to ventilator”
“put a lower value to ventilator”

the ventilator is a dyson model

it speed can go between 1 and 10

idea : if off and if it say the first sentence, it should turn on and set it to 1
if already at max, alexa should tell me it is at max

for slowing down, once it is at minimum speed it should turn off

here is my code

fan_start:
  sequence:
  - data:
      entity_id: fan.rez_de_chaussee
    service: fan.turn_on    
  - data:
      payload_template: '{{states.fan.rez_de_chaussee.attributes.dyson_speed|int}}'
      topic: "/fan/speed"
      retain: "true"
    service: mqtt.publish
fan_stop:
  sequence:
  - data:
      entity_id: fan.rez_de_chaussee
    service: fan.turn_off
  - data:
      payload: '{{states.fan.rez_de_chaussee.attributes.dyson_speed|int}}'
      topic: "/fan/speed"
      retain: "true"
    service: mqtt.publish 
fan_faster:
  sequence:
  - service_template: >
      {% if (states.sensor.fan_speed_mqtt|int) == 0) %}script.fan_faster_poweron
      {% elif (states.sensor.fan_speed_mqtt|int) == 10 %}script.fan_faster_maximum
      {% else %}script.fan_faster_possible
      {% endif %}
fan_slower:
  sequence:    
  - service_template: >
      {% if (states.sensor.fan_speed_mqtt|int) == 0) %}script.fan_slower_poweroff
      {% else %}script.fan_slower_possible
      {% endif %}
fan_faster_maximum:    
  sequence:
  - data:
      entity_id: media_player.my_echo_dot
      message: le ventilateur est déjà au maximum
    service: media_player.alexa_tts
fan_faster_poweron: 
  sequence:   
  - data: {}
    service: script.fan_start
  - data:
      entity_id: fan.rez_de_chaussee
      dyson_speed: '1'
    service: dyson.set_speed
  - data:
      payload: '1'
      topic: "/fan/speed"
      retain: "true"    
    service: mqtt.publish
fan_faster_possible:
  sequence:    
  - data_template:
      entity_id: fan.rez_de_chaussee
      dyson_speed: '{{ (states.fan.rez_de_chaussee.attributes.dyson_speed|int) + 1}}'
    service: dyson.set_speed
  - data:
       payload: '{{states.fan.rez_de_chaussee.attributes.dyson_speed|int}}'
       topic: "/fan/speed"
       retain: "true"    
    service: mqtt.publish    
fan_slower_poweroff:
  sequence:
  - data: {}
    service: script.fan_stop
  - data:
      payload_template: '0'
      topic: /fan/speed   
    service: mqtt.publish
  - data:
      entity_id: fan.rez_de_chaussee
    service: fan.turn_off      
fan_slower_possible:    
  - data_template:
      entity_id: fan.rez_de_chaussee
      dyson_speed: '{{ (states.fan.rez_de_chaussee.attributes.dyson_speed|int) - 1}}'
    service: dyson.set_speed
  - data:
      payload: '{{states.fan.rez_de_chaussee.attributes.dyson_speed|int}}'
      topic: "/fan/speed"
      retain: "true"    
    service: mqtt.publish

unfortunately the service template does not seems to work :confused:
when i check the configuration, i get following error


Invalid config for [script]: expected a dictionary for dictionary value @ data['script']['fan_slower_possible']. Got [OrderedDict([('data_template', OrderedDict([('entity_id', 'fan.rez_de_chaussee'), ('dyson_speed', '{{ (states.fan.rez_de_chaussee.attributes.dyson_speed|int) - 1}}')])), ('service', 'dyson.set_speed')]), OrderedDict([('data', OrderedDict([('payload', '{{states.fan.rez_de_chaussee.attributes.dyson_speed|int}}'), ('topic', '/fan/speed'), ('retain', 'true')])), ('service', 'mqtt.publish')])]
invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['script']['fan_faster']['sequence'][0]['service_template']. Got '{% if (states.sensor.fan_speed_mqtt|int) == 0) %}script.fan_faster_poweron {% elif (states.sensor.fan_speed_mqtt|int) == 10 %}script.fan_faster_maximum {% else %}script.fan_faster_possible {% endif %}\n'
invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['script']['fan_slower']['sequence'][0]['service_template']. Got '{% if (states.sensor.fan_speed_mqtt|int) == 0) %}script.fan_slower_poweroff {% else %}script.fan_slower_possible {% endif %}\n'. (See /home/alain/.homeassistant/configuration.yaml, line 100). Please check the docs at https://home-assistant.io/components/script/

any idea of what I do false ?

Shouldn’t those sensor states end with .state ?

So, for example:


{% if (states.sensor.fan_speed_mqtt.state | int)

and so on

Thanks that would had been the next error i would had in my script, i corrected that.

But this still does not help the issue.

Shouldn’t that be ‘data_template’?

Missing %} after elif

thanks for your help all of your :slight_smile:
there was a lot of errors in my yaml, i splitted it in smaller version and validate each file one by one to get rid of all errors.