Please help with choose within script syntax

I have this script that fullfills a set speed mode service of a template fan. Something isn’t wrong, according to the log:

Invalid config for [script]: Expected a dictionary @ data['sequence'][0]['choose'][0]['conditions'][0]. Got None.

In this script:

ventilation_set_preset_mode:
  mode: single
  fields:
    preset_mode:
      description: Set the fan preset mode (off, stand 1, stand 2, stand 3, stand 4)
  sequence:
    - choose:
        - conditions: >
            {{ is_state('fan.mechanische_ventilatie', 'off') AND preset_mode == 'off' }}
          sequence:
            - stop: "No further action needed."
    - choose:
        - conditions: >
            {{ is_state('fan.mechanische_ventilatie', 'on') AND preset_mode == 'off' }}"
          sequence:
            - service: fan.turn_off
              target:
                entity_id: fan.mechanische_ventilatie
            - stop: "No further action needed."
    - choose:
        - conditions: >
            {{ is_state('fan.mechanische_ventilatie', 'off') AND preset_mode != 'off' }}
          sequence:
            - service: fan.turn_on
              target:
                entity_id: fan.mechanische_ventilatie
            - delay: 30
    - service: esphome.ventilatie_set_speed
      data:
        run_speed: >
          {% if preset_mode == 'stand 1' %}
          1
          {% elif preset_mode == 'stand 2' %}
          2
          {% elif preset_mode == 'stand 3' %}
          3
          {% elif preset_mode == 'stand 4' %}
          4
          {% endif %}
        run_time: 0

But I can’t figure out what. Hope anyone can point me to my error(s).

Thanks!

It’s untestet, but try this:

ventilation_set_preset_mode:
  mode: single
  fields:
    preset_mode:
      description: Set the fan preset mode (off, stand 1, stand 2, stand 3, stand 4)
  sequence:
    - choose:
        - conditions: 
          - condition: template
            value_template: "{{ is_state('fan.mechanische_ventilatie', 'off') AND preset_mode == 'off' }}"
          sequence:
            - stop: "No further action needed."
        - conditions: 
          - conditions: template
            value_template: "{{ is_state('fan.mechanische_ventilatie', 'on') AND preset_mode == 'off' }}"
          sequence:
            - service: fan.turn_off
              target:
                entity_id: fan.mechanische_ventilatie
            - stop: "No further action needed."
        - conditions: 
            - condition: template
              value_template: "{{ is_state('fan.mechanische_ventilatie', 'off') AND preset_mode != 'off' }}"
          sequence:
            - service: fan.turn_on
              target:
                entity_id: fan.mechanische_ventilatie
            - delay: 30
            - service: esphome.ventilatie_set_speed
              data:
                run_speed: >
                  {% if preset_mode == 'stand 1' %}
                  1
                  {% elif preset_mode == 'stand 2' %}
                  2
                  {% elif preset_mode == 'stand 3' %}
                  3
                  {% elif preset_mode == 'stand 4' %}
                  4
                  {% endif %}
                run_time: 0

I think the set speed part should be run after the third options (fan off and preset not off), so the intendation was wrong. And you had the wrong syntax with the conditions.

and is not all caps, it’s and


As a sidebar, you can simplify this based on your logic, and I think you have the last action in the wrong spot which is forcing you to have ‘stops’ all over the place. You can put them all into 1 choose and if your logic flows properly, it will only run the sequences it needs to.

Secondly, you can use variables to make the conditions easier to read. That’s not mandatory.

Finally, you already have the number in your preset modes. No need for the if statement when you can just take the last character as the stand number.

ventilation_set_preset_mode:
  mode: single
  fields:
    preset_mode:
      description: Set the fan preset mode (off, stand 1, stand 2, stand 3, stand 4)
  variables:
    fan_on: "{{ is_state('fan.mechanische_ventilatie', 'on') }}"
    fan_off: "{{ not fan_on }}"
    mode_off: "{{ preset_mode == 'off' }}"
    mode_on: "{{ not mode_off }}"
  sequence:
    - choose:
        - conditions: "{{ fan_on and mode_off }}"
          sequence:
            - service: fan.turn_off
              target:
                entity_id: fan.mechanische_ventilatie
        - conditions: "{{ fan_off and mode_on }}"
          sequence:
            - service: fan.turn_on
              target:
                entity_id: fan.mechanische_ventilatie
            - delay: 30
            - service: esphome.ventilatie_set_speed
              data:
                run_speed: "{{ preset_mode[-1] }}"
                run_time: 0

Thanks very much. How stupid, the cause was indeed the capital AND :frowning:
Thanks also for optimising the script!!

The bottom flow was correct in that sense, it will need to run when none of the choose sections are executed (fan on and mode on) and after the only choose section without a stop.