How to use status panel with Flux Query?

Hello, I am using influxDB as data source in Grafana and using flux query for data. Data is a PLC heart-beat data (boolean) and I wrote a simple logic to convert this timeseries boolean data into a simple status message “Alive” or “Dead”. I have to perform this for 10 PLC’s.

I want to use the ‘Status Panel’ but I cannot make it to work. I do not find sufficient documentation. Here is the query:

from(bucket: “Safety_Alarms”)
|> range(start: -20s)
|> filter(fn: (r) => r._measurement == “safety” and r.name == “Aging_1” and r._field == “heart_beat”)
|> keep(columns: [“_time”, “_value”])
|> sort(columns: [“_time”], desc: true)
|> limit(n: 2)
|> reduce(
identity: {first: true, last: true},
fn: (r, accumulator) => ({
first: if accumulator.first == true then r._value else accumulator.first,
last: r._value
})
)
|> map(fn: (r) => ({
_value: if r.first != r.last then “Alive” else “Dead”
}))

|> rename(columns: {_value: “result”})

I have multiple PLC’s and I want to monitor 1 metric for each of them in this status panel. The above code is for 1 PLC. I would appreciate some help with this task. Thank you.

Assuming that information on all PLCs is stored in one bucket, each PLC has unique value of name tag and that expected result is the following:

One possible solution could look like this:

import "join"

data = <YOUR_DATA_QUERY_FOR_ALL_PLCS>

last = data
|>group(columns:["name"])
|>last()
|>group()

first = data
|>group(columns:["name"])
|>first()
|>group()


join.left(
    left: first,
    right: last,
    on: (l, r) => l.name == r.name,
    as: (l, r) => ({name: l.name, status : if l._value == r._value then "Dead" else "Alive"}),
)