Add running count of specific state changes
Two levels of window functions.
To keep the code short, you can combine the aggregate FILTER clause with the outer window function:

SELECT ts, status
     , 1 + count(*) FILTER (WHERE previous_status = 's' AND status &lt;&gt; 's')
                    OVER (ORDER BY ts) AS flag
FROM (
   SELECT *, lag(status) OVER (ORDER BY ts) AS previous_status
   FROM   tbl
   ) sub;


db&lt;>fiddle here

Assuming current Postgres and all columns NOT NULL.
Related:


Return counts for multiple ranges in a single SELECT statement
Grouping or Window