Give duration to delay in script

I’m quite sure my code once worked. But it currently does not, I saw errors in the protocol, that delay expects other parameters. I’m not sure if I reverted all changed I did while trying to repair, but anyway. I’d like to know what is wrong.

- delay
(last step in my script - so it works without delay currently) gives a message that
Fehler: offset should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F'

Script to activate state
alias: Licht Zustand aktivieren
description: ""
sequence:
  - if:
      - condition: template
        value_template: "{{ is_state(lampe, 'unavailable') }}"
      - condition: template
        value_template: "{{ is_state(schalter, 'on') }}"
    then:
      - sequence:
          - action: switch.turn_off
            metadata: {}
            data_template: {}
            target:
              entity_id: "{{ schalter }}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 3
              milliseconds: 500
          - action: switch.turn_on
            metadata: {}
            data_template: {}
            target:
              entity_id: "{{ schalter }}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 0
              milliseconds: 250
    enabled: true
  - if:
      - condition: template
        value_template: "{{ is_state(schalter, 'off') }}"
    then:
      - action: switch.turn_on
        metadata: {}
        data_template: {}
        target:
          entity_id: "{{ schalter }}"
  - action: scene.turn_on
    target:
      entity_id: >-
        {{ 'scene.' ~ liste ~ '_' ~ states('input_select.' ~ liste ~ '_zustand')
        }}
    data: {}
  - delay: "{{ delay }}"
fields:
  schalter:
    required: true
    description: Steuert ZigBee-Licht
    default: switch.lichtschalter_flur_kg
  lampe:
    required: true
    default: light.keller_hornbach_baumarkt_ag_viyu_a60_806_rgbw_10011725_light
  liste:
    default: flur_kg
    required: true
  delay:
    selector:
      duration:
        enable_millisecond: true
    default:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 100
mode: queued
max: 10

So my field which I’d like to use is defined as this:

fields:
  delay:
    selector:
      duration:
        enable_millisecond: true
    default:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 100

and giving this field as parameter to the delay as this:

- delay: "{{ delay }}"

gives the error that duration does not expect a time, it expects a string.

I mean, OK, I can sure filter it somehow to create a string.

But why is the delay function not expecting a time duration to wait as parameter, but a string which can be converted to a duration? Why did it work before? Which is the correct and most elegant way to do this delay?

What does the debug trace show?

In the Trace there appears the error I already showed:
Fehler: offset should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F'

But it used to work, and in my opinion it makes no sense to implement a delay function that only accepts text (that needs to be converted to a duration internally in the delay function) if a duration is available as data type. That’s why I believe that the message is not the full truth, or maybe that the functionality should be fixed that got broken with some update.

I should have been more clear… what does the trace show for the value of delay in the Changed Variables section?

Also, the default is just for what is shown in the fields UI, it does not actually populate the variable with a value.

image

Perhaps in previous uses you have always selected or passed a value for delay…but in this instance you did not? Again, we would need to check the Trace to see it that’s what is happening.

If you actually need a default make sure to add it wherever the variable is used.

- delay: "{{ delay | default({'hours': 0, 'minutes': 0,'seconds': 0,'milliseconds': 100}) }}"

Templates always return a string. That is in the nature of templates, and cannot be aboided. However, to make this more usable, HA tries to convert the string to some native type if possible. This might be what huts you here. Try to return a dictionary with the hours, minutes, … as in your first code block from your post.