How to implement wait in a script?

Hi everyone, i have a script to turn on HVAC and set it at the right temperature and in ECO mode, yet sometimes it does not work and it stays in AUTO mode… i read about the wait template in the script syntax but i just don’t really understand how to implement it… at the moment i use delay but as i said above they don’t work really well, how could i solve it?
Here’s my script:

accensione_pdc_soggiorno:
  sequence:
    # Accendo PDC
    - service: climate.turn_on
      entity_id: climate.daikin_soggiorno
    - delay: '00:00:05'
    # Imposto temperatura a 20°C
    - service: climate.set_temperature
      target:
        entity_id: climate.daikin_soggiorno
      data:
        temperature: '20'
        hvac_mode: 'heat'
    - delay: '00:00:15'
    # Imposto in modalità Eco
    - service: climate.set_preset_mode
      target:
        entity_id: climate.daikin_soggiorno
      data:
        preset_mode: "Eco"
    - delay: '00:00:05'
    # Ventola in modalità Auto
    - service: climate.set_fan_mode
      target:
        entity_id: climate.daikin_soggiorno
      data:
        fan_mode: "Auto"
    # Avvio timer
    - service: timer.start
      target:
        entity_id: timer.attesa_avvio_pdc
    - delay: '00:00:05'
    # Invio notifica
    - service: notify.christian
      data_template:
        title: '*PDC Soggiorno*'
        message: >
            Accesa in autoconsumo
            
            Lettura Contatore: {{states('sensor.lettura_contatore_filtrata')}} W,
            Temperatura Attuale: {{states('sensor.temperatura_pt')}}°C

This will wait until the change happens, or if it takes longer than 10 seconds it will continue. You could change it to not continue if you wish:

    - service: climate.set_preset_mode
      target:
        entity_id: climate.daikin_soggiorno
      data:
        preset_mode: "Eco"
    - wait_for_trigger:
        - platform: state
          entity_id: climate.daikin_soggiorno
          attribute: preset_mode
          to: "Eco"
      timeout:
        seconds: 10
      continue_on_timeout: true

Another way to do it, try 3 time to change it, with a 5 second wait between tries:

    - repeat:
        sequence:
          - service: climate.set_preset_mode
            target:
              entity_id: climate.daikin_soggiorno
            data:
              preset_mode: "Eco"
          - delay:
              seconds: 5
        until:
          - condition: or
            conditions:
              - condition: state   # stop looping if it changes
                entity_id: climate.daikin_soggiorno
                attribute: preset_mode
                state: "Eco"
              - condition: template
                value_template: "{{ repeat.index > 2 }}" # stop trying after three attempts.
1 Like

Amazing, that’s what i was looking for, thanks!

1 Like