Count and calculate the duration of Boolean true

can anybody guide me to write a tick script? I want to count the number of booleans true and write the result back to a new field. i want to calculate how many time acConnected was true. And also want to calculate duration of acConnected = true Here is my query.

SELECT last("acConnected") AS "acConnected" FROM "test"."autogen"."BatteryReport" WHERE time > :dashboardTime: AND time < :upperDashboardTime: AND "Model"='00575 2019/11/18Hewlett-PackardPrimary' GROUP BY time(30s) FILL(none)

I can write another query with where statement and set asConnected = true but I can not understand the results returned by the query.

Here is query result:
![image|690x166](upload://hyndFOiRpwfR07isUSOuy0eBCDC.png)

SELECT count("acConnected") AS "acConnected" FROM "test"."autogen"."BatteryReport" WHERE acConnected = false AND time > :dashboardTime: AND time < :upperDashboardTime: AND "Model"='00575 2019/11/18Hewlett-PackardPrimary' GROUP BY time(30s) FILL(none)

Here is the query result: