Tech Talks

Store Machine States in SQL: Build a Data Historian with Peakboard

How Peakboard automatically stores machine states in SQL, how a stored procedure secures data consistency and how the SQL LEAD command derives run times and downtime durations from it.

02.07.2026

·

7 min read

Peakboard data historian storing machine states in a SQL database for OEE and downtime analysis.
Key takeaways
  • Peakboard detects state changes at the machine and writes each new state automatically, with a timestamp, into a SQL database.
  • Two tables are enough: one for the current state per machine, one for the complete state history.
  • With the SQL LEAD command, run times and downtime durations are calculated automatically from timestamp pairs.
  • The result: an audit-proof data foundation for OEE, MTBF and downtime analyses – without any additional software.

Peakboard detects every state change at the machine – from RUN to STOP, from SETUP to RUN – and writes it automatically, with a timestamp, into a SQL database. From this state history, run times, downtime durations and OEE values can be calculated directly, without a single figure being entered by hand. That is the Peakboard approach to a real data historian: a lean database schema, a robust stored procedure, clever SQL queries.

What is a data historian and why do I need one?

A data historian is a database that continuously stores machine states and process data with a timestamp. Unlike a simple dashboard that shows only the current state, the historian builds a complete state history: machine X was in state RUN from 06:03 to 07:41, then in state STOP until 08:15, then RUN again.

From this basis, all the important metrics can be derived: total run time per shift, downtime by cause, OEE over weeks, MTBF (Mean Time Between Failures) and MTTR (Mean Time To Repair). Peakboard builds this historian directly on a SQL database, without any additional software.

The database schema: two tables

Two tables are needed for the basic setup:

The first table Machines holds one row per machine with the current state and machine name. It is the fast lookup for the visualization: which machine is running right now, which is stopped?

The second table MachineStateHistory is the real heart of the historian. It contains one row per state change:

  • ID — primary key
  • TS — timestamp of the state change
  • MachineName — machine name
  • State — new state (e.g. RUN, STOP, SETUP)

Every time a machine changes its state, a new row is added. Nothing is ever overwritten. The complete history is preserved.

The stored procedure: SetNewState

Instead of sending UPDATE and INSERT directly from Peakboard, a stored procedure encapsulates the whole logic. On the call, the machine name and new state are passed. The SP then updates the current state in the Machines table and at the same time writes a new entry into MachineStateHistory:

CREATE PROCEDURE [dbo].[SetNewState]
  @MachineName nvarchar(20),
  @State nvarchar(20)
AS
BEGIN
  UPDATE Machines SET state=@State WHERE machinename=@MachineName
  INSERT INTO MachineStateHistory (TS, MachineName, State)
  VALUES(getdate(), @MachineName, @State)
END

Why a stored procedure instead of direct commands? Three reasons: the logic lives in a single place. The operation is atomic – either both writes succeed or neither does. And a single database call from Peakboard is enough instead of two separate commands.

In Peakboard the SP is triggered via a building-block call – for example when an OPC UA signal reports a state change or the operator enters a reason on the touchscreen.

Calculating run times: the SQL LEAD command

The historian table stores timestamps – but not yet durations. To calculate downtime and run times, each record needs the timestamp of the next state change of the same machine. That is exactly what the SQL LEAD command does:

SELECT MachineName, State, TS as TimeStart,
  LEAD(TS) OVER (PARTITION BY MachineName ORDER BY TS) AS TimeEnd
FROM MachineStateHistory

PARTITION BY MachineName ensures that LEAD only jumps within the same machine. The result: every row now has a start time and an end time.

In the next step, DATEDIFF converts the time span into minutes:

WITH results AS (
  SELECT MachineName, State, TS as TimeStart,
    LEAD(TS) OVER (PARTITION BY MachineName ORDER BY TS) AS TimeEnd
  FROM MachineStateHistory
)
SELECT *, DATEDIFF(mi, TimeStart, TimeEnd) as TimeSpanInMinutes
FROM results;

With this result set, Peakboard can aggregate via a dataflow: the sum of all RUN minutes for machine A, the sum of all STOP minutes, frequency by state. From this you get the availability component of the OEE value directly.

From the database to the dashboard

Peakboard reads the SQL result as a data source and processes it further via a dataflow. A dataflow can group the raw data by machine and state, calculate the minute sums and present them as a simple metric on the dashboard. No separate BI tool, no manual aggregation, no delay.

The advantage of this approach over off-the-shelf MES systems: the database schema is self-defined and fully controllable. The stored procedure can be extended at any time – with additional states, with shift IDs, with order references. Peakboard grows with you.

Without your own SQL maintenance: historization via the Peakboard Hub

The SQL route gives you full control over schema and queries – but it assumes you operate and maintain a database yourself. If you do not want that, or if you need to bring machine data from several boxes and sites together centrally, the Peakboard Hub takes over the historization.

The Peakboard Hub stores machine, sensor and manually captured data centrally, instead of only displaying it momentarily. Two building blocks take on the tasks that, in the SQL approach, are handled by your own tables and the stored procedure:

  • Peakboard Hub Lists are central data tables that all connected boxes read and write at the same time. This is where the state history lands, without you creating a database schema yourself.
  • Peakboard Hub Flows are central processes that you define once and run on the Peakboard Hub server – for example capturing every state change, bundling many machine messages or writing metrics back to ERP and MES.

Better or worse is the wrong question here – it is about effort and reach. A single line with its own database is quickly mapped in SQL. But as soon as several boxes, several plants or continuously captured data come together, the Peakboard Hub takes the administration off your hands: from central data storage to the automatic rollout across the entire device fleet.

Connection to OEE and machine monitoring

The data historian is the data foundation for all further analyses. The OEE metric needs run times and downtime data – the historian supplies both. Downtime tracking uses the state history for pattern analysis over weeks. And machine monitoring shows the real-time status – the historian stores everything that happens in real time for later analysis.

If you want to evaluate the historized data not only in Peakboard but also in deeper reports, you can use the SQL database directly as a data source for Power BI – Peakboard shows the live view on the shop floor, Power BI delivers the weekly evaluation for management.

Product

Central data historization with the Peakboard Hub

Store machine states centrally, keep a full history and evaluate them across all boxes and sites – without building and maintaining a database yourself.

What is a data historian?

A data historian is a database that continuously stores machine and process data with a timestamp. Instead of only knowing the current state, you get a complete history of all state changes – the basis for OEE, MTBF and downtime analyses.

Why stored procedures instead of direct SQL commands?

Stored procedures encapsulate the logic of a state change in exactly one place. They are atomic (either both writes succeed or neither), faster than several individual commands and easier to maintain.

How do you calculate downtime durations from timestamp data?

With the SQL LEAD command, each state record is given the timestamp of the next state change of the same machine. DATEDIFF turns this into a duration in minutes – a ready basis for all further aggregations.

Which SQL databases does Peakboard support?

Peakboard supports SQL Server, MySQL, Oracle, PostgreSQL and more. For the data historian, SQL Server is recommended for its native LEAD support and stored procedure functionality.

Do I need my own database for the historian?

Not necessarily. The SQL route gives you full control over schema and queries but requires your own database. Alternatively, the Peakboard Hub handles historization centrally: Peakboard Hub Lists store the state history, Peakboard Hub Flows capture every change – without you building and maintaining a database yourself.

Can I evaluate the historized data with AI?

Yes. The Peakboard Hub provides an MCP server (Model Context Protocol) and thereby becomes a data source for AI agents and LLM applications. This lets you query and analyze the historized machine data directly in a dialog – without a separate export. The classic route via Power BI directly onto the SQL database is also available.

How much storage does a historian need?

With 10 machines at an average of 20 state changes per shift, around 200 rows per day are created – roughly 70,000 rows per year. That is minimal and runs on standard hardware without any problem.

Can the historian log OPC UA data continuously too?

Yes. Peakboard scripts can react to OPC UA value changes and automatically trigger a database write – either on every change or cyclically at defined intervals.

Share this article:
Peakboard Favicon
Author: Peakboard Editorial

The Peakboard editorial team writes about digitalization, data visualization, and process optimization in industry and logistics. The focus is on practical solutions, current developments, and clearly presented expert knowledge.

Snow-covered mountain with orange markings along the summit.
Black and white image of snow-covered mountains in a valley.
White clouds running in horizontal lines against a black background.
White clouds running in horizontal lines against a black background.