---
title: "How to delay an Airflow DAG until a given hour using the DateTimeSensor"
description: "How to use the DateTimeSensor in Airflow"
author: "Bartosz Mikulski"
author_bio: "Principal AI Engineer & MLOps Architect. I bridge the gap between \"it works in a notebook\" and \"it works for 200 million users.\""
author_url: https://mikulskibartosz.name
author_linkedin: https://www.linkedin.com/in/mikulskibartosz/
author_github: https://github.com/mikulskibartosz
canonical_url: https://mikulskibartosz.name/delay-airflow-dag-until-given-hour-using-datetimesensor
---

The DateTimeSensor is another part of the Airflow API that seemingly makes no sense. Why would I want to wait until a specified time? For sure, there is little usage of configuration looking like this:

```python
from airflow.sensors.date_time_sensor import DateTimeSensor

time_sensor = DateTimeSensor(
    task_id='wait_until_new_year',
    target_time='2021-01-01T00:00:00',
)
```

I could imagine only one situation when it is useful. For example, we may have a DAG that will execute only once when we notify the customers that we have just launched a new product. Such mailing will require a few preparation steps, so we can perform those tasks earlier and then wait until the launch date to send the emails.

However, the DateTimeSensor can be used with templates, and that makes it way more powerful. If I have a DAG that must wait until 10 am to execute some tasks, I can configure the sensor using the `execution_date` parameter:

```python
time_sensor = DateTimeSensor(
    task_id='are_we_there_yet',
    {% raw %} target_time='{{ execution_date.replace(hour=10) }}', {% endraw %}
)
```

What is the advantage of this configuration? When I run a backfill for some past date, it will not get stuck at the time sensor (because the date has already passed). If I used TimeDeltaSensor, it would wait the specified number of minutes even during the backfills, which probably makes no sense.