Count the number of system services by state value

I am new to flux and I am looking for help with a query. It may not even be possible.
I am monitoring services on my system and I need to build 3 single stats that represent a count of failed services, stopped services, and running services. Each service is reported individually with their state value every 10s.
The values that represent each state is -1 = failed, 0 = stopped/inactive and 1 = running.
For example, each service looks like this

_value _field _measurement service
1 state agent_services vmpd.service

I have the following flux query for each single stat and I change the “targetState” value for each graph based on the above (-1, 0, 1).

targetState = 1 // running services

sLast = from(bucket: "agent")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "agent_services")
  |> filter(fn: (r) => r["_field"] == "state")
  |> filter(fn: (r) => r._value == targetState)
  |> last()
  |> group(columns: ["state"])
  |> group()
  |> count() // have also tried |> aggregateWindow(every: v.windowPeriod, fn: count, createEmpty: true)
  |> yield()

This query works well as long as there are results that are returned. However, if for example, I have no services that are failed I get “No Results”.
image
What I would like to have instead is to have the single stat default to 0 if there are no results found instead of displaying “No Results”.

Is this possible to do? If any suggestions on how to achieve this?
Thank you for all of your help.

Basically below snippet
“|> filter(fn: ® => r._value == targetState)”
is removing other states. If I am correct to understand, you are removing other states first and expecting zero values at the time of grouping. I am pretty much sure what you are expecting is not possible while you still use above snippet.

Thank you for your reply.
I have 3 different single stat boxes each is using the same query but having targetState changed. So for the “Inactive Local Services” graph, targetState is set to targetState = 0 while the “Failed Local Services” query has targetState = -1. Each single stat the query is only querying for the results for targetState because the others are irrelevant. This is why I have |> filter(fn: (r) => r._value == targetState) in there so that I only pull results where state value is equal to targetState. And the above query does work, but if my polling period for the graphs are 5m and I have had no services report inactive or failed within that period then there are no results returned from the query, which is expected. However, if that condition exists I would like to just display 0 in my single stat instead of “No Results” as seen in my original post.
I guess this is where my inexperience is showing through. What would be a better query to achieve this? If I remove the line |> filter(fn: (r) => r._value == targetState) then how do I only display the number of services whose state value is equal to targetState? Ultimately can I display a 0 count instead of No Results if there are no results returned?

I am not sure whether its possible or not. First filter itself brings empty table. If you some how add data to it with condition, then you could get the value. But not sure its possible.

You could try “set” command once.