Welcome @rw950431 to the Grafana forum.
You did not state your datasource, but mentioned StateDuration, so perhaps you are using InfluxDB with Flux? If yes, perhaps this will help.
And from the InfluxDB forum:
Count how long a value is 1 or 0 (on or off)
Calculating total ON or OFF time for a period (day, month, etc.).
Computing duration of a state using events.duration function