uns-historian — MQTT Historian

Subscribes to MQTT topics and logs every value change to PostgreSQL in real-time. No values missed between polls.

LanguageGo
TypeMQTT function
Scaffolded withfnkit go-mqtt
Depends onMQTT Broker, Valkey, PostgreSQL

What It Does

Reads topics from Valkey config, subscribes via MQTT, and logs every value change to PostgreSQL. In-memory change detection skips duplicate values. Unlike uns-log (HTTP-triggered, poll-based), uns-historian captures every change as it happens.

uns-historian vs uns-log

Aspectuns-loguns-historian
TriggerHTTP (polled on timer)MQTT (real-time)
Data captureSnapshot at poll timeEvery individual change
Missed valuesPossible — between pollsNone
Row contentFull snapshot (all tags)Single value per row

Config in Valkey

docker exec fnkit-cache valkey-cli SET fnkit:config:uns-historian \
  '{"table":"uns_historian","topics":["v1.0/acme/factory1/mixing/line1/#"]}'

Topics support explicit paths and MQTT wildcards (+ single level, # multi-level).

PostgreSQL Table

CREATE TABLE IF NOT EXISTS uns_historian (
  id BIGSERIAL PRIMARY KEY, logged_at TIMESTAMPTZ DEFAULT NOW(),
  enterprise TEXT, site TEXT, area TEXT, line TEXT, tag TEXT,
  topic TEXT NOT NULL, value JSONB NOT NULL );

Quick Start

# Set config in Valkey
docker exec fnkit-cache valkey-cli SET fnkit:config:uns-historian \
  '{"table":"uns_historian","topics":["v1.0/acme/factory1/mixing/line1/#"]}'

# Build and start
cd uns-historian && docker compose up -d

# Check logs
docker logs -f uns-historian