Delay function in automations

Hi,
thanks for suggestion, I already tried this but it don’t seem to solve my problem…If I setup 20 minutes, all work fine, but if I setup 20 hours don’t work: I think if I reboot HASSIO in the interval of 20 hours (very likely) it reset the counter…

this is my automation:

automation:
  alias: "Notifica ritiro indifferenziata"
  trigger:
    platform: numeric_state
    entity_id: sensor.raccolta_indifferenziata_mattino
    value_template: "{{ state.attributes.days }}"
    below: 1
  condition: []
  action:
    - service: notify.famiglia
      data:
        message: "Attenzione: domani è giorno di ritiro indifferenziata."
    - delay: '20:00:00'

Ok, so your description was misleading. The automation doesn’t always trigger at '00:00', that’s apparently just an example. Now I see it is triggered by a sensor changing state. Thanks for the clarification. Of course, given this new information, my original suggestion doesn’t make sense anymore.

Yes, you are correct. If during the 20 hour delay HA restarts, or the automation is turned off and back on, or if automations are reloaded, the automation is reset, meaning it will stop and be re-initialized, and of course, not execute the step(s) following the delay.

BTW, if the automation is triggered while it is waiting in the delay from the last trigger, it will immediately about the delay and execute the next step in the action sequence, which is probably also not what you want.

If you want to make this handle those scenarios, I would suggest replacing the delay step in the current automation with a service call that writes the current time into an input_datetime (with both time and date), and then add another automation that triggers when the time reaches (or exceeds) the date/time recorded in that input_datetime, plus 20 hours.

1 Like

Yeah, what Phil said, except I’d probably store the time 20 hrs from the trigger ie + (20x60x60) do you need it to the second, or is the previous whole minute good enough ?

Another way is to use a timer and this technique to restore active timers.

Currently, delays and timers cannot survive a restart. If either one is mid-way through its countdown when it’s interrupted by a restart, their countdown is lost. The link above shows a way of restoring timers that were active prior to the restart (it can’t restore a delay).

1 Like

thanks for reply.

If I understood I can try this config:

# configuration.yaml entry
input_datetime:
  only_time_organico:
    name: time_organico
    has_date: false
    has_time: true
    initial: '19:45'
  only_time_indifferenziata:
    name: time_indifferenziata
    has_date: false
    has_time: true
    initial: '19:55'

# automation.yaml entry
automation:
  alias: "Notifica ritiro indifferenziata"
  trigger:
    platform: numeric_state
    entity_id: sensor.raccolta_indifferenziata_mattino
    value_template: "{{ state.attributes.days }}"
    below: 1
  condition:
    - condition: state
      entity_id: input_boolean.asciugatrice_switch
      state: 'off'
    - condition: time
      entity_id: input_datetime.only_time_indifferenziata
    # At least one of the following is required.
      after: '19:30:00'
      before: '23:00:00'
  action:
    - service: notify.famiglia
      data:
        message: "Attenzione: domani è giorno di ritiro indifferenziata."
    - service: notify.alexa_media
      data:
        target: 
          - media_player.echo_dot_di_daniele
        data:
          type: announce
        message: "Attenzione: domani è giorno di ritiro indifferenziata."

In your opinion this could work fine?

not work:

Invalid config for [automation]: [entity_id] is an invalid option for [automation]. Check: automation->condition->0->entity_id. (See ?, line ?).

any help to understand error?

Your indentation is incorrect.

No, that’s not anything like I suggested.

Just wonder if there a better solution than an automation triggered by a template trigger that uses sensor.time and compares it with the stored time?

I use a slightly different approach - run a timer instead of delay (with an automation on timer.finish), and on HA restart I start the timer with adjusted duration (as I know when the timer started and what was its duration).

How do you know when the timer started?

every time I (re)start my timer, I store time in input_detetime. and all timers have fixed duration set by input_number.

ok, first part it’s ok:

#configuration.yaml entry
input_datetime:
  datetime_organico:
    name: datetime_organico
    has_date: true
    has_time: true
  datetime_indifferenziata:
    name: datetime_indifferenziata
    has_date: true
    has_time: true


automation:
  alias: "Notifica ritiro indifferenziata"
  trigger:
    platform: numeric_state
    entity_id: sensor.raccolta_indifferenziata_mattino
    value_template: "{{ state.attributes.days }}"
    below: 1
  condition: []
  action:
# Sets date and time to date and time from timestamp (current date and time in this example)
    - service: input_datetime.set_datetime
      data_template:
        entity_id: input_datetime.datetime_indifferenziata
        date: >
          {{ now().timestamp() | timestamp_custom("%Y-%m-%d", true) }}
        time: >
          {{ now().timestamp() | timestamp_custom("%H:%M:%S", true) }}

but how I can do automation2?

another automation that triggers when the time reaches (or exceeds) the date/time recorded in that input_datetime , plus 20 hours.

how about this idea?

you suggest to trigger when input_datetime.datetime_indifferenziata = sensor.date_time+20?

how I can deploy that?

like the?


"{{ states('sensor.date_time') == (((state_attr('input_datetime.datetime_indifferenziat' , 'timestamp')) + (20 * 60 * 60))|timestamp_custom('%H:%M', false)) }}"

sothing like this?


automation:
  alias: "notifica ritiro indifferenziata"
  trigger:
    platform: template
    value_template: "{{ states('sensor.date_time') == (((state_attr('input_datetime.datetime_indifferenziat' , 'timestamp')) + (20 * 60 * 60))|timestamp_custom('%H:%M', false)) }}"
  condition: []
  action:
    - service: notify.famiglia
      data:
        message: "Attenzione: oggi è giorno di ritiro indifferenziata."
    - service: notify.alexa_media
      data:
        target: 
          - media_player.echo_dot_di_daniele
        data:
          type: announce
        message: "Attenzione: oggi è giorno di ritiro indifferenziata."

Close, but that won’t work. Try this:

value_template: >
  {{ states('sensor.date_time').replace(',', '')|as_timestamp >=
     state_attr('input_datetime.datetime_indifferenziat', 'timestamp') +
     20*60*60 }}

you suggest this?

automation:
  alias: "notifica il giorno dopo il ritiro indifferenziata"
  trigger:
    platform: template
    value_template: "{{ states('sensor.date_time').replace(',', '')|as_timestamp >= state_attr('input_datetime.datetime_indifferenziat', 'timestamp') + 20*60*60 }}"
  condition: []
  action:
    - service: notify.famiglia
      data:
        message: "Attenzione: oggi è giorno di ritiro indifferenziata."
    - service: notify.alexa_media
      data:
        target: 
          - media_player.echo_dot_di_daniele
        data:
          type: announce
        message: "Attenzione: oggi è giorno di ritiro indifferenziata."

it don’t work:


automation:
  alias: "notifica il giorno dopo il ritiro indifferenziata"
  trigger:
    platform: template
    value_template: "{{ states('sensor.date_time').replace(',', '')|as_timestamp >= state_attr('input_datetime.datetime_indifferenziat', 'timestamp') + 20*60*60 }}"
  condition: []
  action:
    - service: notify.famiglia
      data:
        message: "Attenzione: oggi è giorno di ritiro indifferenziata."
    - service: notify.alexa_media
      data:
        target: 
          - media_player.echo_dot_di_daniele
        data:
          type: announce
        message: "Attenzione: oggi è giorno di ritiro indifferenziata."

nothing happen…no telegram message…no alexa announce…

I can’t use value_template>, but it only accept value_template:

Have you solved your problem? What you posted isn’t valid. You need to add a dash to the alias line:

- alias: "notifica il giorno dopo il ritiro indifferenziata"

That’s assuming there aren’t other issues with the way you’ve entered the automation into your configuration file(s.)