Configuring MsgFilterSQL in WinCC AlarmControl to Filter Alarms

David Krause10 min read
HMI / SCADASiemensTutorial / How-to
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Overview

The MsgFilterSQL property of the WinCC AlarmControl accepts a subset of SQL that targets the message archive columns rather than user-defined tables. It lets you limit which rows the control displays by message number (MSGNR), class (Type), priority, state, and several other internal fields exposed by the alarm logging database. The companion property DefaultMsgFilterSQL is evaluated at runtime startup and is the correct location for a static filter; MsgFilterSQL itself is typically dynamized via a tag, a script, or a direct connection so that the filter can change while the screen is active.

The most common field-referenced confusion is the difference between user-defined message columns (such as a custom NR field you may have added to a project-specific database) and the system column MSGNR that the alarm logging service writes for every event. MsgFilterSQL queries the internal alarm view, so NR (or any user column) is not visible to it. Use MSGNR for the configured alarm number on the HMI/PLC side, and reserve NR for SQL queries against your own ODBC archive or reporting database.

Prerequisites

  • TIA Portal V16 or later with WinCC Comfort/Advanced or WinCC Professional. The syntax documented here has been verified on TIA Portal V17, V18, and V21 with WinCC Runtime Advanced and WinCC RT Professional.
  • An HMI device configured with an AlarmControl on at least one screen. The screen must be reachable from the start screen so the filter can be validated.
  • Configured discrete alarms or analog alarms with known message numbers. MSGNR matches the ID assigned in the alarm class configuration (HMI alarms) or the MSG_LOCK/MSG_ACK/MSG_NA numbers used by PLC-side alarm instructions (e.g. S7-1500 Program_Alarm).
  • For dynamic filtering: an internal tag of the appropriate data type (typically WString or String) bound to the MsgFilterSQL property, or a VBScript function that rewrites the property on event.
  • Optional but recommended: the official Siemens support entry "How to use the MsgFilterSQL property of the WinCC Alarm Control" (SIOS entry 5668269) as a desk reference.

Column Reference for MsgFilterSQL

The following columns are exposed to the internal SQL parser of the AlarmControl. Only the listed columns should be used; user tables and user fields are not reachable from this property.

Column Description Typical use
MSGNR Configured alarm number from the alarm editor or PLC Program_Alarm/Alarm_8P instance DB. Filter by a single ID or a numeric range.
State Alarm state bits. Values are bitmasked: 1 = came in (raised), 2 = went out (cleared), 4 = acknowledged, 8 = locked. Composite values such as 3, 5, 7, 11 are normal. Show only unacknowledged, raised alarms.
Type Alarm class ID. Standard classes shipped with WinCC: 17 = Errors, 18 = System, 19 = Warnings, 20 = Information, 21 = Diagnostic events, plus user-defined classes created in the HMI alarms editor. Restrict the view to one or more alarm classes.
Priority Numeric priority 0–16 assigned in the alarm configuration. Hide low-priority noise.
TimeComing, TimeGoing, TimeAcknowledged Timestamps as DATE_AND_TIME. Time-range filtering with BETWEEN.
ComputerName Source HMI/PLC station name. Show only alarms originating from a named station.
UserName Login user at the time the alarm was acknowledged or operated. Filter by operator.
Instance Alarm instance (relevant for PLC alarm instructions). Show alarms for a specific instance DB.
Important: Column names are case-insensitive in the WinCC parser, but reserved SQL keywords must be wrapped in square brackets (for example, [UserName]) when used in a WHERE clause.

Supported SQL Operators

WinCC AlarmControl implements a limited SQL dialect. The supported operators are:

  • Comparison: =, <>, <, <=, >, >=
  • Logical: AND, OR, NOT
  • Set: IN ( value, value, ... ), BETWEEN value AND value
  • Null: IS NULL, IS NOT NULL
  • Wildcard on string columns: LIKE 'pattern' using % for any string and _ for any single character

Functions such as SUBSTRING, DATEADD, CAST, and sub-selects are not supported by the MsgFilterSQL parser. For arithmetic on dates, format your comparison with the literal that WinCC writes into the archive.

Syntax: DefaultMsgFilterSQL vs MsgFilterSQL

Both properties accept the same expression text. The difference is lifecycle:

Property When evaluated Typical binding Reset behaviour
DefaultMsgFilterSQL On screen load / alarm control initialisation. Static string entered in the Inspector under Properties → Miscellaneous. Applied once when the screen opens; runtime changes to the property are ignored until reload.
MsgFilterSQL Continuously while the alarm control is visible. Dynamization via tag, script, or direct connection. Re-evaluated every time the tag value or property changes.

If you set both, MsgFilterSQL takes precedence at runtime. The control does not concatenate them; it uses whichever property is currently non-empty. To stack conditions, write a complete WHERE-style expression in a single property.

Filter by Alarm ID: Working Examples

Single alarm number

Show only alarm number 17:

MSGNR = 17

Multiple alarm numbers using AND/OR

Showing only alarm 17 OR 21 (note: a single column cannot equal two different values, so use OR, not AND):

MSGNR = 17 OR MSGNR = 21

Combination of class and number range

Show class 17 (Errors) and class 21 (Diagnostic events), with alarm numbers less than 111:

(Type = 17 OR Type = 21) AND MSGNR < 111

Equivalent expression using IN for the class set:

Type IN (17, 21) AND MSGNR < 111

Range filter using BETWEEN

Show all alarms with numbers between 100 and 199:

MSGNR BETWEEN 100 AND 199

Unacknowledged alarms only

State bits: 1 = raised (came in), 4 = acknowledged. To show raised-but-not-acknowledged alarms use a bitmask test:

(State & 1) = 1 AND (State & 4) = 0
Note: The original question in the source thread incorrectly used AND with two MSGNR equality checks. SQL evaluates MSGNR = 17 AND MSGNR = 21 as a contradiction that always returns FALSE, which is why the filter appeared empty. Use OR for membership across multiple IDs, or the IN (17, 21) shorthand.

Dynamizing MsgFilterSQL with VBScript

For interactive filtering (operator-driven lookups, line-select filters, or PLC-driven scope changes), bind MsgFilterSQL to a VBScript function. The function must return a string. When the returned string is empty, the alarm control shows all messages.

Step-by-step

  1. Open the HMI screen that contains the AlarmControl.
  2. In the Inspector, select the AlarmControl and navigate to Properties → Miscellaneous.
  3. Right-click MsgFilterSQL and choose Dynamic dialog or ... (depends on TIA version), then select VB function.
  4. Create a function with the signature Function GetMsgFilterSQL() As String in the project's Scripts folder.
  5. Implement the filter logic, concatenating columns, operators, and values as a string.
  6. Compile and download. Trigger the function on screen events (e.g., a button's Click event, a tag change) or via a cyclic trigger.

Sample VBScript

Function GetMsgFilterSQL() As String
    Dim sType As String
    Dim sNrFrom As Long
    Dim sNrTo As Long
    Dim sResult As String

    ' Read configured limits from internal tags
    sType   = SmartTags("FilterType")         ' e.g. "17,21"
    sNrFrom = SmartTags("FilterNrFrom")        ' e.g. 100
    sNrTo   = SmartTags("FilterNrTo")          ' e.g. 199

    If sType = "" And sNrFrom = 0 And sNrTo = 0 Then
        GetMsgFilterSQL = ""                  ' no filter, show all
        Exit Function
    End If

    If sType <> "" Then
        sResult = "Type IN (" & sType & ")"
    End If

    If sNrFrom > 0 Or sNrTo > 0 Then
        If sResult <> "" Then sResult = sResult & " AND "
        If sNrTo >= sNrFrom And sNrTo > 0 Then
            sResult = sResult & "MSGNR BETWEEN " & sNrFrom & " AND " & sNrTo
        Else
            sResult = sResult & "MSGNR >= " & sNrFrom
        End If
    End If

    GetMsgFilterSQL = sResult
End Function
Safety check: If the function returns a syntactically invalid expression, WinCC ignores it silently and continues to display all messages. Add a small status output (a separate tag) if the filter appears not to be applied.

Common Errors and Fixes

Symptom Likely cause Fix
Filter returns no rows, no errors shown. MSGNR = 17 AND MSGNR = 21 — always false. Use OR or IN (17, 21).
Filter never updates at runtime. Expression written to DefaultMsgFilterSQL instead of MsgFilterSQL. Move dynamization to MsgFilterSQL.
Filter applied on screen load but disappears on screen change. Filter set via MsgFilterSQL but not re-applied when screen returns. Re-trigger the dynamization on screen load (e.g., a load-event script).
Parser reports unknown column. Custom user column (NR) referenced instead of MSGNR. Use MSGNR. Custom columns require ODBC queries, not MsgFilterSQL.
String comparison never matches. Missing single quotes around string literals. Use ComputerName = 'HMI_LINE_1', not ComputerName = HMI_LINE_1.
Filter behaves erratically on HMI tag values. Tag used in the function not declared as internal, or wrong data type. Declare as internal tag, match data type (e.g., WString).
TIA Portal V21 Unified: property not visible. AlarmControl renamed in WinCC Unified. See WinCC Unified configuration below.

WinCC Unified (TIA Portal V20/V21)

In WinCC Unified Runtime the alarm control is configured under Inspector → Properties → Miscellaneous → Alarm control. The filter fields are similar but the dynamization interface differs:

  • The filter is typically set through JavaScript on the AlarmControl object, e.g. Screen.Items('AlarmControl_1').Filter = "MSGNR > 100".
  • Configuration dialogs for cell and row behaviour are described in the TIA Portal help under Alarm control (RT Unified) - WinCC Unified.
  • WinCC Unified supports the same column names (MSGNR, Type, State, etc.) for the filter property.

The recommended entry point for documentation is the TIA Portal V21 help portal hosted at docs.tia.siemens.cloud.

Step-by-Step: Implementing the Filter

  1. In the project tree, expand HMI → Screens and open the screen containing the AlarmControl.
  2. Select the AlarmControl and open the Inspector window.
  3. Under Properties → Miscellaneous, locate DefaultMsgFilterSQL and enter a baseline expression, for example Type IN (17, 21).
  4. Right-click MsgFilterSQL and select ... (or Dynamic dialog) → VB function. Create GetMsgFilterSQL as shown above.
  5. Define internal tags FilterType (WString), FilterNrFrom (Long), FilterNrTo (Long) in the HMI tag table.
  6. Add I/O fields or a button on the screen that writes to these tags. For example, a "Apply" button sets the tags then triggers the dynamization by raising the tag change.
  7. Compile the HMI and download to the runtime.
  8. Verify with a known alarm number: trigger alarm 17 from the PLC, then confirm it appears in the AlarmControl when the filter is set to MSGNR = 17 and disappears when set to MSGNR = 99.

Verification Checklist

  • Static filter check: Enter a known alarm number in DefaultMsgFilterSQL, restart runtime, confirm only that alarm appears.
  • Dynamic filter check: Change the bound tag value at runtime, confirm the AlarmControl refreshes without a screen reload.
  • Empty filter check: Set the script return value to "", confirm all active alarms reappear.
  • Operator pre-check: Switch WinCC user class; verify that filtering is not gated by user-rights unless intentional.
  • Archive check: The filter affects the on-screen view only. Archived messages in the alarm log remain intact. To filter archived data, use a separate SQL query against the alarm database, not MsgFilterSQL.
  • Audit check: In the GHMIs (Operator audit) or WinCC Audit, ensure the filter change is logged if required for GMP/regulated environments.

Diagnostic Tools and Logging

When the filter behaves unexpectedly, enable these diagnostics before contacting support:

  1. In the WinCC project, navigate to Runtime settings → Logging and increase the alarm logging verbosity.
  2. Use the APDIAG tool (WinCC RT Professional) or the HmiLogViewer (WinCC RT Advanced) to read the runtime log for "MsgFilterSQL" or "AlarmControl" messages.
  3. Capture a screenshot of the AlarmControl with the filter property shown in the Inspector.
  4. Export the alarm configuration to compare MSGNR values against the filter expression.

Performance Considerations

  • MsgFilterSQL is evaluated against the alarm buffer in memory. For systems with very large alarm bursts (> 5000 active alarms), prefer narrow filters at the source (PLC-side MSG_LOCK, MSG_EN) rather than at the display layer.
  • Avoid LIKE '%pattern%' leading-wildcard searches; they force full-buffer scans.
  • Combine multiple OR conditions into IN (a, b, c, ...) where supported — the parser optimizes IN faster than long OR chains.
  • Long expressions (> 256 characters) may hit the property buffer limit on older WinCC versions. Use shorter forms or move logic into the script.

FAQ

What column do I use to filter by alarm number?

Use the system column MSGNR. It matches the configured alarm ID on the HMI or the alarm number generated by PLC instructions such as Program_Alarm. User-defined columns such as NR are not visible to the MsgFilterSQL parser.

Why does my AND between two MSGNR values return nothing?

A single column cannot equal two different values simultaneously. Use OR or the shorthand MSGNR IN (17, 21). Writing MSGNR = 17 AND MSGNR = 21 always evaluates to FALSE.

What's the difference between DefaultMsgFilterSQL and MsgFilterSQL?

DefaultMsgFilterSQL is applied once at screen load and is typically a static string. MsgFilterSQL is the dynamically updated property bound to a tag or a VBScript function. Use DefaultMsgFilterSQL for fixed filters and MsgFilterSQL when the operator or PLC must change the scope at runtime.

Can I filter by Date/Time ranges?

Yes, using TimeComing BETWEEN '2024-01-01 00:00:00' AND '2024-12-31 23:59:59' or numeric comparisons on the timestamp. Match the exact format WinCC writes to the archive; sub-string functions are not supported.

Does the filter delete or hide archived messages?

The filter only affects the on-screen view of the AlarmControl. The alarm archive on disk remains untouched. To restrict what is archived, configure the alarm logging settings separately, not MsgFilterSQL.

Back to blog