Triggering an automation early then the specified time

Hi all

Just wondering if anyone can advise me on triggering an automation before the specified time? I am using the time component to match the time of my sensor and then it fires the automation.

However I experience 5-8 seconds lag with the time component and would therefore like to adjust this issue by triggering my automation 5-8 seconds early so that everything is in sync.

Any help will be appreciated.

Thank you.

Anyone…??

Could you please post your code or at least an example? Otherwise it is just guessing.

So here is my code:

- alias: Manutd Time
  initial_state: 'on'
  trigger:
    platform: template
    value_template: '{{ states.sensor.manutd.state == states.sensor.time.state }}'
  action:
    - service: notify.me
      data:
         message: 'Manutd Playing now'

This is my time sensor:

  - platform: time_date
    display_options:
      - 'time'

And this is the text file that only contains the time in HH:MM format.

  - platform: file
    name: Manutd
    file_path: /home/homeassistant/.homeassistant/python_scripts/manutd.txt

So, you try to compare two time stamps? In my opinion this wont work reliably.

You can compare them like this for example:

trigger:
    platform: template
    value_template: '{% if (states.sensor.time.state ) > ((as_timestamp(now()) - (1*60*5))  | timestamp_custom("%H:%M",true)) and (states.sensor.time.state ) <= ((as_timestamp(now()) )  | timestamp_custom("%H:%M",true))  %}True{% endif%}'

This should give you a range of 5 minutes in this case.

That’s correct all I am doing is to compare 2 time(s) and then fore an automation.

What do u mean by 5mins?

- (1*60*5) reduces the time by 5 minutes.

hmm I see but I wanted the automation to fire 5 seconds before the time. how can I achieve that?

also have you used the time sensor and have you noticed the 5seconds lag?

I do use time as a trigger in automations. I never checked for considerable lag as those automations are not time critical and don’t have to be executed by the second. How do you notice the 5 second lag?

I noticed the lag when the automation triggers. So e.g. if the text file is at 18:00 and when the time sensor hits 18:00 the automation does not trigger by the second rather it triggers after 5-8 seconds.

I noticed this lag with the time sensor itself i.e. when the minute changes it does not change and has a lag of 5-8seconds. So it could be the reason why the automation has a lag.

Do you know how I could reduce the time in seconds ?

Ok, but what do you trigger/execute that shows the lag?

Well I monitor how long it takes for the notification to arrive on my phone after the automation is triggered. Does that make sense?

Just to jump in, I think the 5 second lag you are talking about is unavoidable, since there is some time involved in sending the message to the cloud and it arriving at your phone (I am assuming iOS notifications and these are not processed locally, the cloud is involved).

I, for example, am not using a time-comparison trigger as you are; I send a notification when my garage door opens. Sure enough, the door opens, and several seconds later I get the notification.

1 Like

Exactly. Whatever notification service you are using, it always will have some sort of delay, shorter or longer. So, using HA as a tea timer might not be the best idea. But that is not HAs fault but rather cause by the accumulation of latencies of different systems being used.

1 Like

I am certain it’s nothing to do with the notification instead it’s the time component which is the issue as I have monitored it and when the minutes changel the time on HA frontend lags for about 5 seconds.

What’s exactly the problem? What are your trying to do that 5 seconds delay/lag is such a big issue for getting notified? Which notification component are you using?

1 Like

Thanks for your concern but I have explained myself previously. As I said it’s nothing to do with the notification component.

Template sensors like that have a major performance impact. Instead you should create a binary template sensor that is on when the condition is met.

That may reduce your lag a bit.

1 Like

I have tried to implement it and this is what I have come up with but gives me error in this line {{ is_state('sensor.manutd', is_state('sensor.time')) }}

binary_sensor:
  - platform: template
    sensors:
      test_up:
        friendly_name: "Test"
        value_template: >-
          {{ is_state('sensor.manutd', is_state('sensor.time')) }}

That won’t work, because is_state requires 2 arguments, and you’ve only given it sensor.time.

binary_sensor:
  - platform: template
    sensors:
      test_up:
        friendly_name: "Test"
        value_template: >-
          {{ states.sensor.manutd.state == states.sensor.time.state }}

This assumes that sensor.time isn’t a timestamp with second (or sub-second) accuracy.