Skip to main content

Using sensors in projects

info

dg and Dagster Components are under active development. You may encounter feature gaps, and the APIs may change. To report issues or give feedback, please join the #dg-components channel in the Dagster Community Slack.

Prerequisites

Before following this guide, you will need to create a project with the create-dagster CLI.

Assets and jobs frequently use sensors that are instantiated elsewhere in the project.

For example, if you have created a new Dagster project with dg called my_project, you can define the sensors at src/my_project/defs/sensors.py:

Sensor binding can happen at any level of the defs hierarchy. If you moved asset_one in this example to a subdirectory, you could leave the existing sensors.py file at src/defs/sensors.py:

src
└── my_project
└── defs
├── assets
│ └── asset_one.py # contains def asset_one():
└── sensors.py # contains dg.sensor

Scaffolding sensors

tip

You can scaffold sensors from the command line by running dg scaffold defs dagster.sensor <path/to/sensor_file.py>. For more information, see the dg CLI docs.

To create a sensor dictionary like the above, you can run the following:

dg scaffold defs dagster.sensor sensors.py

which will create

src/<project_name>/defs/sensors.py
import dagster as dg


@dg.sensor(target=None)
def sensor(context: dg.SensorEvaluationContext) -> dg.SensorResult:
return dg.SensorResult()

and you can fill out the sensor dictionary as needed.