Problem with automation or maybe a better way to do this

This automation is generally ok, but I’m confident that sometimes the “delays” dont work. Like, it will make it all the way through the automation in just a few minutes. I cannot see how or why the delays would be ignored, skipped, is there any bug with delays at the moment?? Also, is there a better way to do this? Maybe something more accurate?

- alias: "Notify Paul when its time to leave for work"
  trigger:
  - platform: template
    value_template: >
      {{ is_state('sensor.time_to_leave_for_work1', 'True') and
         is_state('input_select.work_location', 'work1')  or
         is_state('sensor.time_to_leave_for_work2', 'True') and
         is_state('input_select.work_location', 'work2')  }}
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1    
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - condition: time
    after: '06:00:00'
    before: '10:00:00'
  action:
  - service: notify.main_echos
    data_template:
      message: >-
        {% if states('input_select.work_location') == 'work1' %}
          work1d on current traffic, you have 15 minutes to leave if you want to get to work1 by {{ states.input_datetime.work_time_paul.attributes.hour }} {{ states.input_datetime.work_time_paul.attributes.minute }}.  Traffic is {{ states('sensor.pauls_traffic_density_to_work1') }} today.
        {% elif states('input_select.work_location') == 'work2' %}
          work1d on current traffic, you have 15 minutes to leave if you want to get to work 2 by {{ states.input_datetime.work_time_paul.attributes.hour }} {{ states.input_datetime.work_time_paul.attributes.minute }}.  Traffic is {{ states('sensor.pauls_traffic_density_to_work2') }} today.
        {% else %}
          I'm not sure where you're going today
        {% endif %}
  - delay:
      minutes: 5
  - condition: state
    entity_id: input_select.house_mode
    state: 'Home'
  - service: notify.main_echos
    data_template:
      message: "You have about 10 minutes to leave if you want to get to work on time"
  - delay:
      minutes: 10
  - condition: state
    entity_id: input_select.house_mode
    state: 'Home'
  - service: notify.main_echos
    data_template:
      message: "You are now going to be late.  Get moving"

Basically I have this sensor which uses a waze sensor for timing

      time_to_leave_for_work1:
        value_template: >
          {% set t = strptime(states.sensor.time.state,'%H:%M') %}
          {{ ( t.hour + t.minute / 60 ) > ( state_attr('input_datetime.work_time_paul', 'timestamp')/3600 - 0.25 - states.sensor.time_to_work1.state | int / 60) }}
        friendly_name: 'Time to leave for work1'

I guess I could have several of these sensors (this one is for 15 minutes before work). Just looking for some ideas, if anyone else does anything similiar

Is it possible the automation is getting triggered again before it has finished from the first time it was triggered? I.e., the template trigger becomes true, then before the actions have finished, the template trigger changes to false and back to true?

If that is happening, then that would explain it. If an automation is in a delay step, and it is re-triggered, it doesn’t start from the beginning, but rather picks up from the next step after the delay.

See this post where I explain this in more detail and provide a couple of solutions.

You know what I’m sure that’s the cause. I did not realize that if the automation gets retriggered that it just “skips” the delay. Good to know. THANKS!

1 Like

I thought that you couldn’t use “delay” in automations. (At least that is what I have read, and it doesn’t work for me either). The way I do jt is to trigger a script. So make the automation in a way that it runs a script and within that script you can use the delay option.

For examples you can check my github page.
https://github.com/jimz011/HA/blob/master/scripts.yaml

https://github.com/jimz011/HA/blob/master/automations.yaml

1 Like

You can use a delay in the action part of an automation. Basically the action part of an automation is a script. (It’s implemented by the same code that runs scripts.) Although, due to the reasons I mentioned above, sometimes it’s better not to use a delay in an automation.

Ah I didn’t know that, the delay stuff didn’t work for me in automations in the past, so all my “delay” automations are scripts haha. Still I think a compact automation is easier to maintain perhaps.

1 Like