Flux query not returning vehicleState

Hi below is my query but in output it does not return the string value of vehicleState, thats blank-
what should i do ?
stateCounts = from(bucket: “telegraf”)
|> range(start: 2024-05-20T00:00:00Z, stop: 2024-05-21T21:00:00Z)
|> filter(fn: (r) => r[“_measurement”] == “mqtt_consumer”)
|> filter(fn: (r) => r[“_field”] == “vehicleState”)
|> group(columns: [“vehicleState”])
|> count()
|> rename(columns: {_value: “stateCount”})
// Step 2: Calculate the total count of vehicleState occurrences
totalCount = from(bucket: “telegraf”)
|> range(start: 2024-05-20T00:00:00Z, stop: 2024-05-21T21:00:00Z)
|> filter(fn: (r) => r[“_measurement”] == “mqtt_consumer”)
|> filter(fn: (r) => r[“_field”] == “vehicleState”)
|> count()
|> group() // Group by nothing to get the total count
|> sum(column: “_value”)
|> rename(columns: {_value: “totalCount”})
|> findRecord(idx: 0, fn: (key) => true) // Extract the totalCount value
// Step 3: Calculate percentages
stateCounts
|> map(fn: (r) => ({
vehicleState: r.vehicleState,
stateCount: r.stateCount,
totalCount: totalCount.totalCount, // Use the extracted totalCount value
percentage: float(v: r.stateCount) / float(v: totalCount.totalCount) * 100.0
}))
|> yield(name: “state_percentages”)

@data_engineer Looks like vehicleState is a field, so the value of vehicleState is actually stored in the _value column, not the vehicleState column. Before you group by vehicleState, you need to create a vehicleState column, which can be done by duplicated the _value column:

stateCounts =
    from(bucket: "telegraf")
        |> range(start: 2024-05-20T00:00:00Z, stop: 2024-05-21T21:00:00Z)
        |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
        |> filter(fn: (r) => r["_field"] == "vehicleState")
        |> duplicate(column: "_value", as: "vehicleState")
        |> group(columns: ["vehicleState"])
        |> count()
        |> rename(columns: {_value: "stateCount"})

// ... the rest of your query