Kapacitor- Handle Alert criteria based on global state and recurring window with a timer firing every 5 secs with emission of data

My tick file:

    dbrp "xxxxxz"."autogen"

stream
    // Select just the cpu measurement from our example database.
    |from()
        .measurement('stream_periodic_stats__agg')
        .where(lambda: "__agg_period" == '5s')
    |groupBy('__agg_group_by')
    |window()
        .period(1m)
        .every(5s)
        .align()
    |alert()
        .crit(lambda: int("event__count") <  200)
        .message('value for tag {{ index .Tags "component_name" }} field event__count:{{ index .Fields "event__count" }} is < 200')
        // Whenever we get an alert write it to a file.
        .log('/kapacitor/logs/alerts.log')
    |httpOut('dump')
    |influxDBOut()
            .database('xxxxxxxz')
            .measurement('alert_output')

I need to implement this Alerting logic on a global state change: For Every 1 min window interval with 12 periodic outputs(.every(5s)), If a condition is met on 3 out of the 12 periods in that window, set a Global variable ALERT_BAD_STATE if it is not already set to TRUE. If a condition is met on 12 out of the 12 periods in that window, set a Global variable ALERT_BAD_STATE if it is not already set to FALSE.

Alert only set on State change for ALERT_BAD_STATE and only if data is coming in(??). Details:

|window
    .period: 1m
    .every(5s)
    .align()

In 1 min interval(12 periods) from a Start time(? or when Kapacitor started ?),

Trigger Condition:

BAD state: if measured variable (output_fps__count) is present and < 29. if condition is TRUE in 3/12 periods(how ?), global variable ALERT_BAD_STATE set to ON/true.

GOOD state: if measured variable (output_fps__count) is present and >= 30. if condition is TRUE in 12/12 periods, global variable ALERT_BAD_STATE set to OFF/false.

How can I do that as I have to maintain state between each of the 12 intervals of the 1 min window ?

I have to redo this again for the each subsequent 1 min window with 12 intervals of 5 secs each.

TIA,