# mes.spGetAssetStateEvents

## Description
Retrieves raw asset state event data by invoking the internal table-valued function `fnGetAssetStateEvents`. Includes adjusted start time logic to include state changes that begin prior to the selected time window.

## Syntax
<b>mes.spGetAssetStateEvents @assetId, @rangeStart, @rangeEnd, @durationSecs, @showChanged, @showDowntimeOnly</b>  
<br /><br />

<b>Parameters:</b><br />
@assetId (int): ID of the asset to evaluate.<br />
@rangeStart (datetime): Start of the time window.<br />
@rangeEnd (datetime): End of the time window. Future dates are truncated to current time.<br />
@durationSecs (int): Minimum duration (in seconds) for a state event to be included.<br />
@showChanged (bit): Whether to include changed state events (1 = include, 0 = exclude).<br />
@showDowntimeOnly (bit): Whether to restrict results to only downtime events (1 = yes, 0 = no).<br /><br />

<b>Returns:</b><br />
Full result set from <b>fnGetAssetStateEvents</b>, including detailed columns like:<br />
- assetId<br />
- assetPath<br />
- assetName<br />
- stateCode<br />
- stateName<br />
- stateCategoryName<br />
- stateTypeName<br />
- startDate, endDate<br />
- stateDurationSecs, stateDurationMins<br />
... and more.

### Code Example

```sql
-- Retrieve detailed state events for asset 3055 in July 2025
EXEC mes.spGetAssetStateEvents 
     @assetId = 3055,
     @rangeStart = '2025-07-01',
     @rangeEnd = '2025-07-31',
     @durationSecs = 60,
     @showChanged = 1,
     @showDowntimeOnly = 0;
