WinCC VBScript DateTime Sync: HMIRuntime vs SmartTags

David Krause13 min read
HMI ProgrammingSiemensTutorial / 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

1. Overview: VBScript and Synchronized Time in TIA Portal WinCC

Synchronizing the panel clock to a coordinated value, writing the local PC clock to an HMI tag, or pushing a calculated timestamp to the PLC are common requirements on SIMATIC HMI projects. Siemens has shipped VBScript as a built-in scripting language in TIA Portal WinCC since WinCC V13, exposing either the HMIRuntime object hierarchy (WinCC Professional / WinCC Runtime Professional) or the SmartTags collection (WinCC Comfort, WinCC Advanced, WinCC Runtime Advanced) depending on the runtime edition. The error pattern reported in the field typically takes the form "Object doesn't support this property or method: 'HMIRuntime'" or "Wrong number of arguments or invalid property assignment" and is almost always caused by mixing the two object models, by selecting the wrong tag data type, or by attempting to use object syntax inside a script that runs in the wrong runtime context.

Microsoft announced the deprecation of VBScript in two phases: the language is in maintenance mode for the Windows 11 / Windows Server lifecycle and will be retired and removed from future Windows releases. The exact statement from the Microsoft Windows IT Pro Blog is that "VBScript will be retired and eliminated from future versions of Windows." New WinCC scripts should therefore be written as conservatively as possible (small surface area, minimal reliance on deprecated COM), and migration plans should consider C#/VB.NET in WinCC Professional or system functions / scheduled tasks in Comfort/Advanced. Background on the language itself is available in the VBScript (Wikipedia) entry.

2. WinCC Edition Comparison: Which Object Model Applies

The first step in any VBScript debugging session is to confirm the engineering target. The same script snippet produces different errors depending on whether the runtime is a Comfort Panel, a WinCC Runtime Advanced, or a WinCC Runtime Professional. The table below maps Siemens HMI runtime to scripting object model and project file type.

WinCC Edition Typical Targets Object Model TIA Portal Project Type
WinCC Comfort Comfort Panels (TP700–TP2200, KTP, IPC) SmartTags HMI → WinCC Comfort
WinCC Advanced RT Advanced on PC, Multi Panel, all Comfort targets SmartTags HMI → WinCC Advanced
WinCC Professional RT Professional on PC, Plant Intelligence, archive/professional options HMIRuntime HMI → WinCC Professional
Rule of thumb: If the project was created from "Add new device → HMI → WinCC Professional", use HMIRuntime.Tags(...). If the project is "HMI → WinCC Comfort/Advanced", use SmartTags(...). Mixing them produces runtime errors that point to an "unknown method" rather than a clear cause.

3. Object Models: HMIRuntime vs SmartTags

WinCC Professional exposes the full HMIRuntime object with a Tags collection, a Screens collection, an Alarms collection, and several process-related collections. The canonical pattern to read and write a tag is:

Dim oTag
Set oTag = HMIRuntime.Tags("MyTag")
oTag.Read
oTag.Value = oTag.Value + 1
oTag.Write

The Read call pulls the current value from the connection into the local cache, and Write pushes the cached value back. Skipping the Read when reading a value that has been changed by the PLC produces stale data; skipping Write after a local change produces no PLC update.

WinCC Comfort and Advanced expose a flat, name-indexed SmartTags collection. The pattern is significantly shorter and does not require explicit Read/Write:

SmartTags("MyTag") = 42
x = SmartTags("MyTag")

There is no HMIRuntime object in Comfort/Advanced, and there is no SmartTags collection in Professional. The same source line that compiles in one project will not compile in the other.

4. Date_And_Time Data Type Requirements

The PLC-side counterpart of an HMI DateTimeLong tag is the SIMATIC DATE_AND_TIME data type (also written Date_And_Time or DT), which is an 8-byte BCD structure in the format YYYY-MM-DD-HH:MM:SS.mmm with two unused high nibbles. When binding a VBScript Date or Now result to an HMI tag that is connected to a PLC address of type DT, the HMI tag itself must be configured with the matching data type in TIA Portal:

Use Case HMI Tag Data Type PLC Partner VBScript Source
Human-readable timestamp on the HMI String (32 chars min) or DateTime STRING / WSTRING FormatDateTime(Now, vbGeneralDate)
PLC clock sync push from panel Date_And_Time (8 bytes) DATE_AND_TIME (DT) CDATE conversion required
Unix epoch in seconds (SCADA, OPC UA, log timestamp) Int / DInt / LReal DInt / LReal DateDiff("s", "01/01/1970", Now)

The most common source of "Type mismatch" errors is assigning a VBScript Date directly to an HMI tag configured as Date_And_Time. The conversion function CDATE wraps a Date into a variant subtype that the WinCC tag driver can map to DT. In Comfort/Advanced this can be hidden by the Date + Time shortcut SmartTags("DateTimeLong") = Date + Time, which works because the operator returns a Date subtype directly. Always verify the tag's PLC connection in the Inspector window: a tag with no PLC connection accepts any value type at runtime; a tag with a connection to a DT address enforces 8 bytes of BCD.

5. Synchronized Time Source Strategies

Before writing a script that pushes the local clock to the PLC, decide which clock is the master. The wrong choice silently produces drift between stations.

  1. PLC as master, HMI as follower (recommended): Use a CP with NTP, an S7-1500 with time-of-day synchronization via PROFINET, or read RD_SYS_T on a 1200/1500 and let the panel read it as the tag value. The HMI is then a viewer, not a source. This is the only configuration in which the panel can be rebooted without losing the time base.
  2. HMI as master, PLC as follower: Use a WinCC Runtime Professional on a domain-joined PC with NTP, push SmartTags / HMIRuntime values to a DT tag, and let the PLC copy it into its system clock via WR_SYS_T (S7-300/400) or SET_CLK extension blocks. This is the original code in the source post.
  3. Independent clocks with periodic alignment: Each node runs its own clock and a script periodically computes an offset. This pattern is fragile; prefer options 1 or 2.
Field caveat: NTP over NT protocol on S7-300/400 is not the same as the more familiar NTP you would see in a Linux box. The "NT protocol" referenced in the field report is the Siemens proprietary time-synchronization protocol carried on top of Ethernet (UDP port 5000 family) between a CP/IE and an HMI panel. It does not speak IETF NTP. If the goal is to point the panel to a public NTP server, use WinCC Runtime Professional on a Windows host with the host OS NTP client and let WinCC read the OS clock via Now; for direct NTP to a PLC use a CP with NTP support (S7-1500 with CP 1543-1, ET 200SP, or S7-1200 with CM/CP).

6. Writing DateTime via HMIRuntime (WinCC Professional)

The following script is the WinCC Professional equivalent of the snippet in the source post. It writes the current local time to a tag named DateTimeLong and pulses a Boolean trigger that the PLC can latch to know "a new value has been written."

' WinCC Professional RT - VBScript
' Tag "DateTimeLong" must be Date_And_Time with PLC connection OR String
' Tag "Trigger" must be Bool with PLC connection

Sub WriteDateTime()
    Dim oDateTime
    Dim oTrigger
    
    Set oDateTime = HMIRuntime.Tags("DateTimeLong")
    Set oTrigger  = HMIRuntime.Tags("Trigger")
    
    oDateTime.Value = CDATE(FormatDateTime(Now, vbGeneralDate))
    oDateTime.Write
    
    oTrigger.Read
    oTrigger.Value = True
    oTrigger.Write
End Sub

Notes for the Professional variant:

  • The Dim ... As Variant declaration is implicit; HMIRuntime.Tags(...) returns a HmiTag object, not a Variant holding the value.
  • Do not call .Read on a tag you intend to overwrite. Read pulls the value of a tag whose value was changed by the PLC while the script was editing a local copy; the call is unnecessary when the next line is .Value = ....
  • The script must be attached to a scheduled task, an event of a screen object, or a global action. A bare function definition in the project does not run.

7. Writing DateTime via SmartTags (WinCC Comfort / Advanced)

For WinCC Comfort Panels and RT Advanced the same logic collapses to the answer given in the field report. Date and Time are VBScript built-ins; their sum is a Date variant that WinCC maps to the configured tag type.

' WinCC Comfort/Advanced RT - VBScript
' Tag "DateTimeLong" data type: Date_And_Time

Sub WriteDateTime()
    SmartTags("DateTimeLong") = Date + Time
End Sub

This works because the + operator on two VBScript Date subtype variants returns a Date subtype, and the HMI tag driver performs the type conversion to Date_And_Time on the way to the PLC. If DateTimeLong is configured as String, the runtime performs an implicit ToString conversion that produces a locale-dependent format; for predictable PLC-side parsing, format the string manually:

SmartTags("DateTimeLong") = _
    Year(Now) & "-" & _
    Right("0" & Month(Now), 2) & "-" & _
    Right("0" & Day(Now), 2) & " " & _
    Right("0" & Hour(Now), 2) & ":" & _
    Right("0" & Minute(Now), 2) & ":" & _
    Right("0" & Second(Now), 2)

The double-digit padding via Right("0" & n, 2) avoids a common bug where Month(Now) = 6 writes "6" instead of "06".

8. Scheduled Execution and Triggering

The Trigger tag in the source code is the recommended pattern for "a new value is available, please read it on the PLC side." To make the script run on a schedule, configure a scheduled task in TIA Portal:

  1. Project tree → Languages & Resources → Scripts: confirm the function name matches the runtime call (e.g., WriteDateTime).
  2. Open the HMI device → Schedules → add a new task.
  3. Trigger type: Cyclic with interval 1000 ms, or Once on time for a single alignment at startup.
  4. Event: Run script → select WriteDateTime.
  5. For Comfort/Advanced the scheduled-task UI is identical; for Professional the equivalent is Global Actions → Schedules.

To avoid pulse-stretching bugs on the PLC side (the PLC samples Trigger faster than the script clears it), write a toggling pattern in the script:

' Toggle trigger so the PLC sees both edges
Dim bNow
bNow = SmartTags("Trigger")
SmartTags("Trigger") = Not bNow

The PLC can then detect either edge with a single-bit edge interrupt (S7-1500: IO_EDGE; SCL: R_TRIG / F_TRIG) instead of comparing to a fixed True level.

9. VBScript Deprecation: Migration Path

Microsoft's deprecation notice changes the long-term calculus for greenfield projects. Practical implications for the patterns above:

  • WinCC Runtime Professional remains a Windows host. As long as the OS image carries the VBScript COM DLLs (default on Windows 10 LTSC 2019 and Windows 11 22H2 as of the cut-off), the existing scripts run unchanged. Future Windows builds may drop the DLLs; the deprecation announcement explicitly states that "all the dynamic link libraries (.dll files) of VBScript will be removed."
  • WinCC Comfort / Advanced run on a hardened Linux-derived RT (SIMATIC WinCC RT Advanced on IPC) and a Windows IoT / Win32 host. Comfort/Advanced panels are not affected by the Microsoft deprecation in the same way the PC runtime is, but the long-term direction is still toward script-free logic (system functions, scheduled tasks, C# via OPC UA in newer TIA Portal versions).
  • Migration candidates: Replace tag read/write VBScript with system functions on tags (e.g., Set value on a tag with formula or event-driven update). Replace scheduling with the built-in Scheduler. For logic that cannot be expressed in functions, port to C# in a WinCC Professional custom control or to SCL in the PLC, keeping the HMI as a viewer rather than a logic node.
Audit tip: Before any panel is replaced or any Windows host is upgraded past the VBScript removal threshold, run TIA Portal → Project → VBScript usage inventory (or grep .vbs under the project source). Scripts that simply read/write a single tag are usually a one-line rewrite to a system function; scripts with complex control flow are the candidates that justify a C# port.

10. Troubleshooting Matrix

Symptom Likely Root Cause Resolution
"Object doesn't support this property or method: 'HMIRuntime'" Script runs in Comfort/Advanced RT Switch to SmartTags object model
"Object variable not set" on Set oTag = HMIRuntime.Tags(...) Tag name typo or tag deleted from project Verify tag exists in the HMI tag table; names are case-sensitive
"Type mismatch: 'CDATE'" or value is empty string Tag is configured as Date_And_Time but assigned a String or vice versa Match tag data type to script variant subtype; use FormatDateTime for String targets
Script runs but PLC never sees a value Tag has no PLC connection or connection is offline Check HMI connection in Inspector; check CP online state
Trigger is stuck at TRUE, PLC never re-fires Script writes True every cycle and PLC samples faster than the tag is reset Use the toggle pattern from §8
Date rolls over a day, time stays at 00:00:00 Tag is configured as Time (4 bytes) instead of Date_And_Time (8 bytes) Change tag data type to Date_And_Time (DT)
Time zone off by N hours VBScript Now returns local time, not UTC Compute UTC manually with DateAdd("h", -Bias, Now) or call a system function on the HMI
Script compiles but does not run Not attached to a schedule or event Add a schedule or call from a button Click event

11. Verification Steps

After deploying the script, confirm correct operation in this order:

  1. Compile clean: TIA Portal → right-click the HMI device → Compile → Software (rebuild all). Any undefined function name is reported here, not at runtime.
  2. Simulator run (Comfort/Advanced): Start the RT simulator from TIA Portal → Start runtime button. Add the tag to a screen with a date/time output field. Confirm the value updates on schedule.
  3. Tag trace (Professional): Use the WinCC tag online monitor (RT Professional → Tools → Tag simulation or the HMI tag table in online mode) to confirm the write happens at the scheduled interval.
  4. PLC-side verify: In the S7 program, copy the incoming DT to a DB and watch it in online mode. If using RD_SYS_T as the master, compare to a known-good value to compute the offset.
  5. Edge detection: Confirm the PLC edge interrupt fires on every scheduled cycle, not just the first one. A single edge in 60 seconds on a 1-second schedule indicates a stuck-trigger bug.
  6. Time zone sanity: Compare the panel display to a known UTC reference (e.g., w32tm /monitor on a domain controller). If they differ by an integer number of hours, the script is not normalizing to UTC.

Why does "HMIRuntime" fail in my WinCC project?

HMIRuntime is exposed only in WinCC Professional and WinCC Runtime Professional. If the HMI device is a Comfort Panel or RT Advanced, switch to the SmartTags object model: SmartTags("DateTimeLong") = Date + Time for Date_And_Time tags, or SmartTags("DateTimeLong") = FormatDateTime(Now) for String tags. The error message itself ("Object doesn't support this property or method") is the diagnostic; the resolution is to use the object model that matches the project type created in TIA Portal.

What HMI tag data type should I use for a PLC DATE_AND_TIME?

Configure the HMI tag as Date_And_Time (8 bytes) with the PLC connection pointing to the DT address (e.g., DB1.DBD0, length 8). For a human-readable string mirror, add a second tag of type String (32+ chars) and write the formatted version. Assigning a VBScript Date directly to a String tag works but yields locale-dependent output; pre-format with Year/Month/Day for ISO-8601 output.

Is VBScript still supported in TIA Portal / WinCC?

Yes, as of the last TIA Portal release. WinCC Professional and WinCC Comfort/Advanced continue to ship a VBScript engine. Microsoft has, however, announced that VBScript will be retired and its DLLs removed from future versions of Windows (see the Windows IT Pro Blog notice). PC-based runtimes are the most exposed to this. Plan a migration to system functions, scheduled tasks, or C# in custom controls where feasible.

How do I make the script run on a schedule?

In TIA Portal, open the HMI device and add a Schedule (Comfort/Advanced) or a Global Action (Professional) with a cyclic trigger of 1000 ms. Set the event to Run script and select the function name. For a one-shot at startup, use the OnChange trigger on a startup tag, or the Once on time schedule type.

Why does my PLC see the same timestamp every cycle?

The script is writing the value but the PLC is not detecting a change. Use a toggling trigger pattern (write Not bNow) so the PLC can detect either edge with a single-bit edge interrupt. A level-based trigger (always TRUE) only fires once on the rising edge and never re-fires until the PLC resets the bit, which the script never does.

Back to blog