WinCC Unified scripting · Problem and fix
Export to CSV from a WinCC Unified script: paths that actually work
Device and storage
Export_20261204_0900.csv
text/csv · UTF-8
Path in the script
'/home/industrial/HMIx-Exports/Export.csv'- The only folder scripts may write to in the panel's memory.
- Use the absolute path, starting with /.
“Can I have last night's data in Excel?” The script looks trivial: build a few lines, call WriteFile. Then nothing appears, or a number like 2156935121 shows up, or the USB stick comes back with an empty file. The cause is almost never the CSV. It is where you write it.
- The allowed folders on Unified PC, Comfort Panel, SD card and USB
- Why the file lands on the server and not on your laptop
- A tested script that exports logged values to a CSV Excel reads cleanly
HMIx team7 min readUpdated
What you see
Four ways a CSV export fails
- Nothing at all. No file, no message. The script had no
.catch, so the error code went nowhere. - A long number. The catch prints something like
2156935121. That is an error code (0x809037D1 in hexadecimal), andHMIRuntime.GetDetailedErrorDescription()turns it into words. - An empty file on the USB stick. The script said it finished, the operator pulled the stick, and the file on the PC is empty or missing.
- The file is “somewhere else”. The operator exported from a web client and the file is not on their laptop.
Avoid· Three problems in five lines
// Pasted from a forum, runs on a Comfort Panel, writes nothing:for (const value of values) { HMIRuntime.FileSystem.AppendFile('\\\\server\\exports\\log.csv', value + ',\n', 'utf8');}// no .catch: the error code is lost, the operator sees nothingWhere a script may write
Server side, absolute paths, and folders the device allows
Rule one: every script runs on the server. Siemens: “you can only access files on the server and not on the file system of the client on which the script was triggered.” Siemens Tips §5.8 A web client exporting a CSV writes it on the machine that runs the Runtime.
Rule two: each device has its own allowed folders.
| Device | Where | Path in the script |
|---|---|---|
| Unified PC | A local folder the Runtime may write to | C:\\Users\\Public\\Exports |
| Unified Comfort Panel | Internal memory, only this folder | /home/industrial/Exports |
| Panel, SD card | Slot X51 | /media/simatic/X51/Exports |
| Panel, USB stick | Ports X61 to X64 | /media/simatic/X61/Exports |
| Network share | Not supported in Runtime scripts | — |
- Double the backslashes on Windows. In a JavaScript string
\starts an escape:C:\\Users\\Publicis what you type forC:\Users\Public. - Use absolute paths on panels. Siemens: relative paths “always refer to the root directory of the system”.
- Create the folder first.
HMIRuntime.FileSystem.CreateDirectory()before the first write; if the folder exists, carry on. - Not straight under C:\. Siemens warns that creating folders straight under C:\ may be rejected for lack of permissions. Pick a folder the Runtime's user can write to.
- No network drives. Siemens does not support file access to network drives from Runtime scripting, for security reasons. Export locally and let another tool move the file.
Write it once, and listen to the answer
WriteFile is asynchronous. Treat it that way.
WriteFile and AppendFile return a Promise. The script carries on, and the result arrives later in .then or .catch. Two habits make all the difference: build the whole text first and write it once (one AppendFile per row is one file operation per row), and always end with a .catch that says what failed.
Do this· One write, one answer
const path = '/home/industrial/Exports/log.csv'; // Comfort Panelconst text = lines.join('\r\n'); // build first...HMIRuntime.FileSystem.WriteFile(path, text, 'utf8') // ...write once .then(() => HMIRuntime.Trace('[CSV] saved ' + path)) .catch((reason) => HMIRuntime.Trace('[CSV] ' + path + ': ' + HMIRuntime.GetDetailedErrorDescription(reason)));For logged values, read them with a LoggedTagSet. The names are “HMI tag:Logging tag”, the read is asynchronous, and the values arrive in blocks of up to 100 per logging tag, so collect them before writing Siemens object model.
Example· Reading the last 24 hours of a logging tag
// Logged values: "HMI tag:Logging tag", read asynchronouslyconst logs = HMIRuntime.TagLogging.CreateLoggedTagSet(['Tank1_Level:Tank1_Level_Log']);const end = new Date();const begin = new Date(end.getTime() - 24 * 60 * 60 * 1000);logs.Read(begin, end, false) .then((results) => { for (const block of results) { // blocks of up to 100 values for (const v of block.Values) { // v.Value, v.Quality, v.TimeStamp } } }) .catch((reason) => HMIRuntime.Trace('[Log] ' + HMIRuntime.GetDetailedErrorDescription(reason)));A CSV that Excel opens in columns
Separators, decimal commas and accents
Excel guesses the separator from the computer's regional settings. In Spain, Germany, France or Italy it expects a semicolon and a decimal comma; with commas, the whole line lands in column A. Try it:
Same data, two separators
Excel set to
Separator in the file
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Time,Logging tag,Value,Quality | |||
| 2 | 2026-12-04 09:00:05,Tank1_Level:Tank1_Level_Log,41.5,192 | |||
| 3 | 2026-12-04 09:00:30,Tank1_Temp:Tank1_Temp_Log,18.25,192 | |||
| 4 | 2026-12-04 09:01:05,Tank1_Level:Tank1_Level_Log,42.25,192 |
Everything lands in column A.
- Match the separator to the readers.
;and decimal comma for continental Europe,,and decimal point for English settings. - Start with \uFEFF. That invisible mark tells Excel the file is UTF-8, so °C, µS and accented tag comments survive.
- Write dates Excel understands.
2026-12-04 09:00:05sorts and filters; a localized long date does not.
USB sticks, and when not to script at all
The last two traps
Pulling the stick too early. Operators report empty or missing files when the stick is removed right after the script finishes. Siemens' EjectStorageMedium system function only works correctly for a medium used for logging, so do the simple thing: show “Export finished, you may remove the stick” only in the .then, and ask the operator to wait for it.
Maybe you do not need a script. To export a whole tag log, the system function ExportTagLog (V21) already writes it to a storage path, with no code to maintain. A script makes sense when you need your own columns, your own selection of tags or a file name your customer imposes.
Ready-to-use script
Export the last hours of logged tags to CSV
Reads the logged values of the last 24 hours, groups the blocks by tag, sorts them by time and writes one dated CSV with a header, semicolons, decimal commas and the UTF-8 mark. Creates the folder, catches every error and tells the operator the result.
- Where it goes
- Button > Events > Click left mouse button
- Runs on
- WinCC Unified V18 or later
Do this· hmix-export-logged-tags-csv.js
// Where the file goes. The script runs on the device that runs the// Runtime, so the file lands THERE, never on a web client's PC.// Unified PC ............ 'C:\\Users\\Public\\HMIx-Exports'// Unified Comfort Panel . '/home/industrial/HMIx-Exports'// SD card (X51) ......... '/media/simatic/X51/HMIx-Exports'// USB stick in X61 ...... '/media/simatic/X61/HMIx-Exports'const FOLDER = 'C:\\Users\\Public\\HMIx-Exports';const LOGGED = ['Tank1_Level:Tank1_Level_Log', 'Tank1_Temp:Tank1_Temp_Log'];const HOURS = 24;const SEPARATOR = ';'; // ';' + decimal comma for a Spanish or German Excelconst DECIMAL_COMMA = true; // false for an English Excel (',' and '.')// Helpers that write dates and numbers the way the file needs themconst two = (n) => String(n).padStart(2, '0');const stamp = (d) => d.getFullYear() + '-' + two(d.getMonth() + 1) + '-' + two(d.getDate()) + ' ' + two(d.getHours()) + ':' + two(d.getMinutes()) +Three ways HMIx saves you the trial and error
- 01
Describe what the script must do. HMIx asks for your tags, writes it, explains it part by part and simulates it.
- 02
Paste a script you already have. HMIx explains it and flags, for free, the classic errors.
- 03
Something fails? HMIx walks you through the checks, from the trace message to the fix.
Frequently asked questions
Where can a WinCC Unified script write files on a Comfort Panel?
Only under /home/industrial on the internal memory, or on external media: the SD card at /media/simatic/X51 and USB sticks at /media/simatic/X61 to X64, depending on the port. Use absolute paths.
Why is my exported file not on the PC of the web client?
Because WinCC Unified runs every script on the server. HMIRuntime.FileSystem only accesses the file system of the device that runs the Runtime, never the client that triggered the script.
What does error 2156935121 mean in WriteFile?
It is an error code returned by the rejected Promise (0x809037D1 in hexadecimal). Pass it to HMIRuntime.GetDetailedErrorDescription() (V18 and later) to get the description in words; forum reports link it to paths the Runtime is not allowed to write to, such as network shares.
Can WinCC Unified write to a network drive from a script?
No. Siemens does not support file access to network drives from Runtime scripting, for security reasons. Write locally and move the file with another tool.
Why does Excel put my whole CSV in one column?
Because the separator does not match the regional settings of that Excel. Continental European settings expect semicolons and decimal commas; English settings expect commas and decimal points.
How do I export a tag log to CSV without a script?
From V21, the system function ExportTagLog exports a tag log to a storage path. Use a script when you need your own columns, tag selection or file name.
Ready to use, from the HMIx catalogue
Free to download: this article’s script, shared from HMIx, and controls for the screen around it.

Runtime scriptFree
Exports 24 hours of logged values to CSV
This same export, shared from HMIx: a free download with its PDF documentation, the steps in TIA Portal and a rehearsal with example data.
Open
Custom Web ControlFree
Historical Data CSV Exporter
Prefer a screen where the operator picks the time window, previews the data and exports? This Custom Web Control does it, with your script feeding the records.
OpenSources
Every Siemens statement in this article was read in the document linked here. Versions and behaviour can change with updates: check the manual of your own version.
- Siemens: SIMATIC WinCC Unified Tips and Tricks for Scripting (JavaScript), entry 109758536, V5.0 (09/2025)
- Siemens: Runtime scripting, General information (WinCC Unified V21.1)
- Siemens: Reading and writing text files (WinCC Unified V21.1)
- Siemens: LoggedTagSet.Read() (WinCC Unified V21.1 object model)
- Siemens: ExportTagLog system function (WinCC Unified V21.1)
- Siemens: EjectStorageMedium system function (WinCC Unified V21.1)
- Siemens: Reading ErrorCodes and ErrorObjects (WinCC Unified V21.1)
- WinCC Unified export CSV
- WinCC Unified write file
- HMIRuntime.FileSystem.WriteFile
- Unified Comfort Panel USB
- /home/industrial
- logged tags CSV
- TIA Portal