# mes.fnGetStateSeconds

## Description
Returns total duration in seconds for a given state type within a time range for an asset.

## Syntax
  <b>mes.fnGetStateSeconds(@assetId, @rangeStart, @rangeEnd, @stateTypeId)</b>  
  <br /><br />
  <b>Parameters:</b><br />
  <li>@assetId (int): Asset Id</li>
  <li>@rangeStart (datetime): Start of the time range</li>
  <li>@rangeEnd (datetime): End of the time range</li>
  <li>@stateTypeId (int): State type Id</li>
  <br />
  <b>Returns:</b><br />
  <li>int: Total seconds in the specified state</li>

### Code Example

```sql
SELECT mes.fnGetStateSeconds(
  2076, 
  '2025-07-30T00:00:00', 
  '2025-07-30T12:00:00', 
  2
) AS seconds_in_state;
