Triggering an automation after a specific time

alias: After 7pm
description: ""
trigger:
  - platform: template
    value_template: "{{ now().hour >= 19 }}"
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.fan
mode: single

Hello, I want to switch off a fan after 7 pm. Above is the automation I am using. It works fine and switches off the fan at 7 pm. But in case there is no power lets say between 6:54 pm to 7:15 pm then this automation is not fired when Home-Assistant is started after the power cut. What am I doing wrong here ? How can make this automation to work if the power comes after 7pm.

I can use a trigger a Home-Assistant started and then check for time. But what is wrong in the above trigger I used. Wouldn’t that trigger get fired if hour>=19 ??

Please guide.

Trigger at a set time and trigger on start up, then condition between the times you want it off.

bonjour,
déclencher une automation toutes les 5 minutes (trigger)
verifier si c’est apres 19h (condition)
eteindre en cas de verification (action)
je crois que

{{ now().hour >= 19 }}

ne sert de trigger qu’au moment d’égalité ou juste de depassement de 19h

Why is it needed to trigger on startup ? Wouldn’t home-assistant check for the below trigger lets say at 20:00 hours if the power comes back at 19:45 ??

- platform: template
   value_template: "{{ now().hour >= 19 }}"

I have no idea what your template is going to do,triggering at start up is just what I would do.

If Home Assistant is off during a power outage at either 6:54 pm to 7:15 pm`, it wont be able to trigger at the specified times. When starts later, it will have missed the trigger time and so will not set the switch to the correct state.

The automation needs to be redesigned to handle a third trigger that occurs when Home Assistant starts. Add the following trigger to your automation:

 - platform: homeassistant
    event: start

Once a Template Trigger is triggered, it must first be “reset” before it will trigger again. The “reset” occurs when the template evaluates to false. It’s the change from false to true that serves to trigger it. In other words, it must first be false before it can change to true.

From the Template Trigger’s documentation:

Your Template Trigger won’t evaluate to false until 00:00.

I suggest you use this version:

alias: After 7pm
description: ""
trigger:
  - platform: time
    at: '19:00:00'
  - platform: homeassistant
    event: start
condition:
  - condition: template
    value_template: "{{ now().hour >= 19 }}"
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.fan
mode: single
  • The first trigger triggers the automation at 19:00:00, the requirements of the Template Condition are fulfilled, and the switch is turned off.

  • If Home Assistant starts anytime between 19:00:00 and (moments before) 00:00:00, the second trigger will trigger the automation, the condition’s requirements are met, and the switch is turned off.

If you use Home Assistant > start trigger, you will probably want to use a wait_template to ensure everything needed loaded properly, otherwise your switch might not flip either (unknown/unavailable if integration has yet to fully load and report back to HA).

action:
  - wait_template: "{{ has_value('switch.fan') }}"
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.fan

Something like this should also work (change the times appropriately). Trigger either at time, or when device comes back available as on:

alias: After 7pm
description: ""
mode: single
trigger:
  - platform: time
    at: "19:00:01"
  - platform: state
    entity_id:
      - switch.fan
    from:
      - unavailable
      - unknown
    to: "on"
condition:
  - condition: time
    after: "19:00:00"
    before: "07:00:00"
action:
  - service: switch.turn_off
    target:
      entity_id: switch.fan
    data: {}

thank you guys for the guidance.

1 Like

Just to clarify:
Assume I need to trigger an automation ONCE after 10 minutes after HA startup.
Is it a proper code?

trigger:
  - platform: template
    value_template: >-
      {% set TIMEOUT = 10 -%}
      {%- set LAST_BOOT = states('sensor.ha_last_boot') -%}
      {%- set MINUTES_PASSED = (as_timestamp(now()) - as_timestamp(LAST_BOOT)) / 60 -%}
      {{ MINUTES_PASSED >= TIMEOUT }}

where “'sensor.ha_last_boot” provided by Uptime.

I.e a logic is:

  1. The automation is started after HA startup.
  2. Before 10 minute it is not triggered.
  3. After 10 minute it is triggered (“false” → “true”).
  4. Then the automation is not triggered any more.

did you consider creating an event on startup, and next, trigger off of that custom event?

  - id: run_at_startup
    trigger:
      platform: homeassistant
      event: start
    action:
      - delay:
          minutes: 10
      - event: delayed_homeassistant_start

  - id: run_at_delayed_startup
    trigger:
      platform: event
      event_type: delayed_homeassistant_start
    action:

specifically for the homeassistant start event, I’ve had this for years now, and it never failed

1 Like

Thanks for a feedback.

I was thinking about using events:
– create an automation raising an event at XXX minutes;
– then create automations listening to this event.

But - mainly for educational purpose - I wanted to create a blueprint which has input values:
– timeout in minutes after HA startup;
– list of actions
because I wanted to have a possibility to “postpone” actions for different timeouts.

Also I was thinking about using a delay - kind of:

    trigger:
      platform: homeassistant
      event: start
    action:
      - delay:
          minutes: !input input_TIMEOUT
      - ... here call a list of actions

But I was not sure what is more effective - triggered at a particular time (“platform: template”) or calling a “delay” function.
“Effective” - means “uses less resources”.

my guess would be the even creation and next trigger off that event would be more efficient, because it does not have to evaluate the template at all.

Having said that, my second guess would be that it would practically be impossible to measure that efficiency :wink:

fwiw, my 2 automations indeed do what you intend to do:

  - id: run_at_startup
    trigger:
      platform: homeassistant
      event: start
    action:
      - if:
          condition: state
          entity_id: input_boolean.run_profiler_at_startup
          state: 'on'
        then:
          service: profiler.set_asyncio_debug
          data:
            enabled: true
      - service: input_boolean.turn_on
        target:
          entity_id: input_boolean.just_started
      - service: script.notify_startup
      - delay:
          seconds: >
            {{states('input_number.ha_delayed_startup')}}
      - event: delayed_homeassistant_start

  - id: run_at_delayed_startup
    trigger:
      platform: event
      event_type: delayed_homeassistant_start
    action:
      - service: script.notify_delayed_startup
      - service: script.run_after_delayed_startup
      - service: input_boolean.turn_off
        target:
          entity_id:
            - input_boolean.just_started
            - input_boolean.run_profiler_at_startup
#       - service: homeassistant.enable_config_entry
#         data:
#           config_entry_id: c9265c192165a4faa889bda950f09d3e

and set a few booleans I can use in other backend logic.
Have no experience with BP’s though so cant suggest improvements there

Well, I hope so.
Temporarily changed my blueprint for using a “delay”.

As for this:

      - delay:
          seconds: >
            {{states('input_number.ha_delayed_startup')}}
      - event: delayed_homeassistant_start

This may be changed for a blueprint:

      - delay:
          seconds: !nput input_VALUE_DELAY_IN_SECONDS
      - choose:
          - conditions:
              - condition: template
                value_template: true
            sequence: !input input_LIST_ACTIONS

or just (untested)

      - delay:
          ...
      - sequence: !input input_LIST_ACTIONS

(do not remember why I chose the 1st way…)

Just noticed: if an automation uses a “delay” instead of a templated trigger - then after a manual forced starting the automation this delay is also executed.
So, both ways (delay after start OR templated trigger) have some “pro” & “contra”.

1 Like