How to get tracked entities with no events for a specific programStage in Tracker API?

I’m working with the DHIS2 Tracker API and I need to retrieve a list of tracked entities (clients) that have no events at all in a specific programStage. In my application, the goal is to show only clients who are enrolled in a program but have not yet created any event in that particular programStage.

So far, I’ve been using the /api/tracker/trackedEntities endpoint with parameters like program, orgUnit, orgUnitMode=DESCENDANTS, programStage, fields=enrollments[events], and event-related filters such as eventOccurredAfter, eventOccurredBefore, and eventStatus. For example:
/api/tracker/trackedEntities?program=somePRG&orgUnit=someORG&orgUnitMode=DESCENDANTS&fields=trackedEntity,enrollments[events[event,programStage,status]]&eventOccurredAfter=2024-01-01&eventOccurredBefore=2024-12-31&eventStatus=ACTION

What I found is that I can successfully retrieve events using /api/tracker/events, and I can also retrieve tracked entities that have events. However, I cannot find any way to retrieve tracked entities that have no events at all in a specific programStage. I also tried filtering enrollments.events as empty (enrollments.events:), but that is not supported. The filter parameter only works for attributes, not for events or enrollments, and event date filters only work when events already exist.

So my question is: is there any supported way in the DHIS2 Tracker API to retrieve tracked entities that have no events in a specific programStage, or to get entities where the events list is empty for that stage? Or is the only recommended approach to fetch events separately and then exclude those tracked entities on the client side?

Hi,
I had the same issue to identifi enrollnts where we have a stage without event.
I finally created program Indicators to count the number of active or completed events in a program stage, and use those in line List to filter the tracked Entitites with Nb of events = 0.

For now, If I need the UIDs of tracked entities or Enrollments, I had to use event Report list as I never managed to get the UIDs from lineList Exports. Hope this helps, and curious to know if there are other workaronds possible

Thanks for the suggestion, but this approach is on the analytics level. I need to work with live data through the Tracker API, so unfortunately this solution doesn’t fit my use case.

In this case I will be very interested if there is a way to do so as i often had issues when trying to filter on “null” valules through API