TIA Portal Data Block Retain Configuring Persistent PLC Variables

David Krause13 min read
SiemensTechnical ReferenceTIA Portal
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

Retentive (retain) memory is one of the most frequently misunderstood areas when commissioning S7-1200 and S7-1500 controllers in Siemens SIMATIC S7-1200 / S7-1500 projects with TIA Portal. The original engineer question boiled down to three concerns:

  1. Do PLC tag variables get cleared at power-off?
  2. How are individual values retained across a STOP/RUN or power-cycle transition?
  3. How do you move one data block value to another value within the same DB using the MOVE box?

Each of those questions has a precise answer that depends on the data block type (classic vs. symbolic), the firmware version (V10.5 SP2, V11, V13, V15.1, V16, V17, V18, V19, V20), and whether the controller is an S7-1200 or S7-1500. This reference compiles the field-proven answers, the official Siemens documentation anchors, and a verified procedure for retentive configuration, DB-to-DB move operations, and ARRAY element addressing with the THIS keyword.

Engineering rule of thumb. In TIA Portal V10.5 SP2 and V11 (the era of the field report discussion), only entire data blocks could be flagged retentive when the DB was created in "classic" (non-optimized) mode. Symbolic / optimized blocks (FB/DB with "Optimized block access" ticked) introduced individual tag-level retain attributes, but only on S7-1200 firmware V4.0+ and S7-1500 from the start.

PLC Tag Retain Behavior

Every PLC tag declared in the default tag table has its retentive attribute defaulting to non-retain. At a CPU STOP->RUN transition, an MRES, or a power-off/power-on cycle, non-retentive tags are re-initialized to the configured start value (0 by default for numeric types, FALSE for BOOL). This is the most common reason commissioning engineers see production counters, recipe indexes, and last-batch IDs "forgotten" by the PLC.

Retentive tags, by contrast, are backed up by the CPU's remanent memory area, which is sustained across the following events:

  • Routine STOP -> RUN transitions
  • Power-off / power-on (provided the backup battery or capacitor is healthy)
  • Firmware restart triggered from the online portal

Retentive tags are not preserved across a memory reset (MRES) or a firmware update that includes a configuration change — those actions explicitly clear the retain area.

Enabling the Retain Attribute on a PLC Tag

The syntax in the declaration editor is exposed in the Retain column and may be written as a property in SCL source view:

VAR RETAIN
    i_ProductionCount : INT;       // retained across STOP/RUN and power-off
    r_LastFlowSetpoint : REAL;     // retained across STOP/RUN and power-off
END_VAR

Limitations apply to PLC tag retention. On S7-1200 CPUs, only global PLC tags in the tag table, the I/O image, and the static section of an FB are individually settable to retain; FC temporary variables and OB temporary variables are never retained. S7-1500 CPUs extend the option to include instance DBs of optimized FBs at element granularity.

Data Block (DB) Retain Behavior

A data block is the preferred container for retain memory in TIA Portal because it groups logically related variables and supports both whole-block and tag-level retention. Whether you can mark individual tags within a DB as retentive depends on two factors: the access mode and the firmware / portal version.

Classic (Non-Optimized) Data Blocks

Classic DBs are identified by the absence of the Optimized block access tick in the block properties. They are addressed with absolute addresses (e.g., DB8.DBW0) and map one-to-one to byte addresses in the S7 work memory. In V10.5 SP2 and V11, classic DBs only support all-or-nothing retention: the Retain attribute in the block properties is set globally. If you tick it, every tag inside the DB is backed up; if you clear it, none are.

Symbolic (Optimized) Data Blocks

There is a small footprint penalty: the symbolic DB's retained area is stored in the work memory, and the load memory is updated only on a block-consistent download. This is a non-issue for the typical machine builder, but it is a constraint to keep in mind when scaling to large retain areas on S7-1200 CPUs with limited memory.

Comparing Classic vs Symbolic Retain Behavior

Property Classic DB (V10.5 SP2+) Symbolic / Optimized DB (V14+)
Granularity of retain Entire DB only Per tag
Address access Absolute (DBW, DBB, DBD) Symbolic only (no DBW)
Default in modern TIA No (legacy) Yes
S7-1200 / S7-1500 support Both Both (S7-1200 fw V4.0+)
Individual retain tick per tag Not available in V10.5 Available
Copyable from SCL using MOVE Yes Yes

Retain Memory Sizing on S7-1200 / S7-1500

Retain capacity is a hardware-limited resource and exceeding it raises a diagnostic buffer entry and prevents the PLC from going to RUN. The S7-1200 and S7-1500 ranges are summarized below.

CPU Work Memory (Programs) Work Memory (Data) Retain Memory (typical)
CPU 1211C / 1212C 50 KB 50 KB 10 KB
CPU 1214C / 1215C 100 KB 100 KB 10 KB
CPU 1217C 150 KB 250 KB 20 KB
CPU 1505S / 1505SP 500 KB program 3 MB data Up to 1.5 MB
CPU 1518-4 PN/DP 6 MB program 60 MB data Multiple MB

The total retain area consumed by all retained tags, DBs, and bit memory markers (M marked as retentive) must not exceed the figure above. For S7-1500, TIA Portal will issue an offline compile error pointing at the cumulative retain size before the project is downloaded.

Moving Data Within a Data Block

The original question was: "How do I move one DB tag to another DB tag? The MOVE box does not seem to work with DB variables." In every tested TIA Portal version (V10.5 SP2 through V20), the answer is consistent: the MOVE box fully supports moving data block tags to other data block tags provided the input and output are of compatible types. If the box shows red datatypes, the issue is not the source or destination — it is a type mismatch.

Verified Procedure (LAD/FBD)

  1. Open the network in the program block (OB, FB, FC) where you want to perform the move.
  2. Insert the Move box from the basic instructions palette (or type MOVE in SCL).
  3. Click the EN input if you want conditional execution; otherwise leave it open.
  4. On the left input, type the source symbol exactly as it appears in the DB declaration. For a classic DB: "Data_DB".tag1 or absolute DB8.DBW0. For a symbolic DB: "Data_DB".tag1.
  5. On the right output, type the destination symbol in the same format, e.g., "Data_DB".tag3.
  6. Confirm the box stays blue (no type-mismatch flags). A blue ??? indicates a missing tag reference; a red ?INT indicates a type conflict.

Worked Example

DATA_BLOCK "Data_DB"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
NON_RETAIN
   STRUCT
      tag1 : UINT;     // non-retained source
      tag2 : INT;
      tag3 : UINT;     // destination
      tag4 : INT;
   END_STRUCT;
END_DATA_BLOCK

The corresponding LAD network moves tag1 into tag3:

|   MOVE                            |
| EN            tag1      -->  tag3  |
|                                       |

If the MOVE box is red and refuses the same-type pair (UINT -> UINT), drop in a CONV box to coerce. The CONV (Convert) instruction accepts a wider range of mismatches and the result is identical for a like-to-like conversion.

|   CONV_UINT_TO_UINT                |
| EN       tag1   -->  INT_value      |
|                                       |
|   MOVE                            |
| EN     INT_value   -->  tag3        |

SCL Equivalent

"Data_DB".tag3 := "Data_DB".tag1;
// or, with type-coercion safety
INT_value := UINT_TO_INT("Data_DB".tag1);
"Data_DB".tag3 := INT_TO_UINT(INT_value);

Why the MOVE Box Can Look "Broken" on DB Tags

  • Data block not yet compiled or downloaded. A brand-new DB that has never been compiled into the SCL/DB sources is invisible to the dropdown. Compile first with Project > Compile > Software (rebuild all).
  • Wrong source DB instance. The MOVE box points at an FB's instance DB (e.g., "Motor_1_Instance".Speed) rather than the global data block. In that case the symbol resolves to the instance, not the global DB, and the data never lands where expected.
  • Type width mismatch across nested STRUCTs. Trying to move a 4-byte DWord into a 2-byte Word position produces a red ??WORD on the destination. Pad with a SLICE or use CONV.

Addressing ARRAY Elements with THIS in S7-1500

For optimized ARRAY data blocks on S7-1500, you can address an element of the current DB from within its own SCL block by using the THIS keyword. This pattern, documented in the official Siemens TIA Portal V20 help on addressing tags in ARRAY data blocks (S7-1500), lets the block self-reference without needing the DB name and survives DB renames in the project tree.

Declaration

DATA_BLOCK "Recipe_DB"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
NON_RETAIN
   STRUCT
      RecipeIndex : INT;
      Values : ARRAY[1..50] OF REAL;
   END_STRUCT;
END_DATA_BLOCK

Accessing an Element

// read the i-th value of the ARRAY from within the DB or an FB/FC
rCurrentValue := THIS[5];

// assign to a specific element
THIS[RecipeIndex] := 100.0;

The THIS token acts as an alias for the current data block's instance and is supported from TIA Portal V14 upwards on S7-1500. It cannot be used outside an SCL block that has implicit access to a DB instance. When using THIS in a global DB, the implicit instance is the DB itself; when using it inside an FB, the implicit instance is the FB's instance DB.

Boundary check. Always validate the index against the ARRAY bounds before dereferencing. A 6 when the ARRAY is [1..5] will raise an area-length error and stop the OB. Use IF iIdx >= LOWER_BOUND(THIS) AND iIdx <= UPPER_BOUND(THIS) THEN ... guards in SCL.

Step-by-Step: Creating a Retained Symbolic Data Block

The following procedure walks through the modern recommended pattern: a symbolic DB with per-tag retention, used as a recipe and runtime-state container.

  1. In the project tree, right-click Program blocks > Add new > Data block.
  2. Name the DB Retained_DB, leave type as Global DB, click OK.
  3. Open the new DB. In the Properties > Attributes tab, tick Optimized block access.
  4. In the declaration table, add a column for Retain if it is not already visible (right-click header > Show/Hide columns > Retain).
  5. Add a tag, for example iBatchCount : INT;. Click the empty Retain cell and select Set retain. The cell shows a small battery icon.
  6. Repeat for every tag you want to retain. Tags left blank in the Retain column are non-retain.
  7. Compile the project (Ctrl+B) and download to the PLC.
  8. In an OB, use MOVE to copy "Retained_DB".iBatchCount to a non-retained work tag if you need a working copy, or simply read the retained value directly.

Step-by-Step: Moving a DB Value to Another DB Value

  1. Create or open the source DB (e.g., Source_DB) and the destination DB (e.g., Dest_DB). Both can be symbolic/optimized.
  2. Open the OB1 or FB where the move will be triggered (e.g., on first scan or on a rising edge of a request bit).
  3. Insert a MOVE box from the Basic instructions > Move operations palette.
  4. Drag Source_DB.tagA from the project tree onto the IN pin, or type it directly into the pin label.
  5. Drag Dest_DB.tagB onto the OUT1 pin.
  6. If the box turns red, inspect the datatypes shown in the pin tooltips. Use CONV to cast.
  7. Compile, download, and monitor the box online to verify the ENO output is green and the source value equals the destination value.

Verification Procedure

After configuration, validate the retain and move setup with the following checks before sign-off:

  1. Online watch table. Force the source tag to a known value, perform the MOVE, and confirm the destination tag updates within a single scan. Use the Monitor all button in the watch table to see live values.
  2. STOP/RUN test. Set the PLC to STOP from TIA Portal, then back to RUN. The retained tag should still hold its last value; non-retained tags should reset to their start values.
  3. Power-cycle test. Disconnect field power for 5 seconds, reconnect, and verify retain behavior matches the documentation. S7-1200 requires the optional battery cartridge (BB1297) for power-off retention; without it, retain is lost on power-off but kept on STOP/RUN.
  4. MRES test. Perform a memory reset. All retained and non-retained data should clear, the PLC should request a fresh download.
  5. Diagnostics buffer. Open Online > Diagnostics > Diagnostics buffer after each test. Look for entries with ID 0x001F "Retentive data inconsistent" or 0x013E "Retain memory full" — both indicate configuration problems.

Troubleshooting Matrix

Symptom Likely Cause Remediation
PLC tag resets on STOP/RUN Retain attribute not set on the tag Click the battery/key icon in the tag table; recompile; redownload
Retain values lost on power-off (S7-1200) No battery cartridge installed Install BB1297 battery, or accept the limit and back up the value in a recipe DB on shutdown
MOVE box shows red datatype Type mismatch (e.g., DWord into Word) Insert CONV; or pad/truncate the source value with SLICE
MOVE destination stays 0 EN is FALSE, or wrong DB instance referenced Verify the enable rung; check the symbol tooltip for the actual DB it resolves to
Compile error: Retain area exceeds memory Total retain size exceeds CPU capacity Reduce the number of retained tags, switch some to non-retain, or upgrade the CPU
ARRAY index out of bounds fault THIS[] used with an unchecked index Add a LOWER_BOUND/UPPER_BOUND guard before the assignment
Cannot tick retain per-tag in V10.5 SP2 DB is classic mode Convert the DB to symbolic/optimized access (if firmware permits), or split into a separate retained DB
Retain values lost after firmware update Firmware change clears retain on signature mismatch Save retain data to recipe DB before update; restore from recipe DB after update

Best Practices

  • Default to symbolic / optimized DBs. Modern TIA Portal versions and S7-1200/S7-1500 CPUs both handle symbolic DBs efficiently, and the per-tag retain attribute is worth the small migration cost.
  • One retained DB per logical function. Grouping recipe data, runtime counters, and operator-setpoints into separate retained DBs makes the retain footprint auditable and reduces the risk of an "unknown" retained tag accumulating in the project.
  • Document the retain intent in the DB header comment. A short comment like "iBatchCount: retain — survives STOP/RUN and power-off" prevents the next engineer from accidentally toggling the attribute off.
  • Use THIS in SCL for ARRAY element access. It removes the dependency on the DB name and survives project tree renames.
  • Avoid retained strings. STRINGs in a retain area can inflate the retain footprint unpredictably; use fixed-length CHAR arrays if you must retain text.
  • Back up critical data to a recipe DB before firmware updates. Firmware mismatches can reset retain. The recipe DB can be loaded from the HMI after the update.

Migration Notes from V10.5 SP2 / V11

Engineers maintaining projects that were created in TIA Portal V10.5 SP2 or V11 should plan a migration if any of the following hold:

  • The project uses classic DBs with all-or-nothing retention and would benefit from per-tag retention.
  • The project relies on absolute DB addressing (DB8.DBW0) that must be preserved across the upgrade.
  • You need to use the THIS keyword for ARRAY element addressing (introduced in V14).

Migration is done with Project > Migrate project. The tool preserves all classic DBs as legacy and lets you selectively re-create them as symbolic / optimized once the project is on the new portal. Always re-validate retain behavior with the verification procedure above after migration.

Do PLC tag variables get cleared at power-off in TIA Portal?

By default, yes. PLC tags without the Retain attribute are reinitialized to their start values (typically 0) on STOP-to-RUN, MRES, or power-off. To preserve values across these events, set the Retain attribute (battery/key icon) on the tag or store the value in a retentive data block.

How do I keep a value through a power cycle in a TIA Portal data block?

Open the DB properties, enable the Retain attribute for the whole DB (classic mode) or tick Retain on individual tags (symbolic/optimized mode), then compile and download. On S7-1200, install the BB1297 battery cartridge for power-off retention; on S7-1500, retain is preserved in non-volatile memory by default.

Why does the MOVE box turn red when I use DB tags?

The MOVE box turns red when the source and destination data types differ. Verify both pins show the same type (UINT, INT, REAL, BOOL, etc.). If the types are intentionally different, insert a CONV (Convert) box between the source and the MOVE input to cast the value.

Can I retain only some tags in a classic data block in TIA Portal V10.5 SP2?

No. In V10.5 SP2 and V11, classic (non-optimized) data blocks only support all-or-nothing retention. To retain a subset of tags, either create a separate retentive DB and copy values into it on first scan, or upgrade the project to V14+ and use a symbolic/optimized DB with per-tag retention.

How do I address an element of an ARRAY data block in S7-1500 SCL?

Use the THIS keyword followed by the index in square brackets, for example: rValue := THIS[5]; or THIS[iIndex] := 100.0;. The THIS token refers to the current DB instance and is supported in TIA Portal V14 and later on S7-1500. Always guard the index with LOWER_BOUND and UPPER_BOUND checks to avoid area-length errors.

Back to blog