Subscribe to a Variable's Last Value

Use the /lv topic to receive a variable's value immediately, then on every change.

Use the last value (/lv) topic to subscribe to a variable's most recent value. On connect you immediately receive the current value, then get a new message whenever the value changes.

Topic structure

/v1.6/devices/{DEVICE_LABEL}/{VARIABLE_LABEL}/lv
ParameterDescription
{DEVICE_LABEL}Your device's unique label
{VARIABLE_LABEL}The variable you want to monitor
/lvLast-value suffix
⚠️

If the device or variable doesn't exist, the subscription will fail.

Connection parameters

FieldValueRequired
Hostapi.surfact.comYes
Port1883 (no TLS) / 8883 (TLS)Yes
UsernameYour Surfact tokenYes
PasswordAny character or blankNo
Quality of Service0 or 1No

Examples

Command line (mosquitto_sub)

mosquitto_sub \
  -h "api.surfact.com" \
  -t "/v1.6/devices/weather-station/temperature/lv" \
  -u "$SURFACT_TOKEN" \
  -p 8883 \
  -q 1

Output:

24.0

Python (paho-mqtt)

import paho.mqtt.client as mqtt

token = "your-surfact-token"
device_label = "weather-station"
variable_label = "temperature"

def on_connect(client, userdata, flags, rc):
    topic = f"/v1.6/devices/{device_label}/{variable_label}/lv"
    client.subscribe(topic)
    print(f"Subscribed to {topic}")

def on_message(client, userdata, msg):
    print(f"Last value: {msg.payload.decode()}")

client = mqtt.Client(client_id="my-device-12345678901234567890")
client.username_pw_set(username=token, password="")
client.on_connect = on_connect
client.on_message = on_message
client.connect("api.surfact.com", 8883)
client.loop_forever()

JavaScript (mqtt.js)

const mqtt = require('mqtt');

const token = 'your-surfact-token';
const deviceLabel = 'weather-station';
const variableLabel = 'temperature';

const client = mqtt.connect('mqtts://api.surfact.com:8883', {
  username: token,
  password: '',
  clientId: 'my-device-12345678901234567890'
});

client.on('connect', () => {
  const topic = `/v1.6/devices/${deviceLabel}/${variableLabel}/lv`;
  client.subscribe(topic, (err) => {
    if (!err) console.log(`Subscribed to ${topic}`);
  });
});

client.on('message', (topic, message) => {
  console.log(`Last value: ${message.toString()}`);
});

Behavior

When you subscribe to an /lv topic:

  1. Immediate delivery — you receive the variable's current last value right away.
  2. Updates — you receive new values whenever the variable is updated.
  3. Retained messages — the last value is always available (see the retain flag).

Use cases

  • Device initialization — get the current state immediately on connect
  • State syncing — ensure your device matches the cloud state on startup
  • Dashboard displays — show the latest value without waiting for an update
  • Control systems — initialize actuators to their last known state
📘

Need the timestamp and context too? Subscribe to the full dot topic instead.


Did this page help you?