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
| Parameter | Description |
|---|---|
{DEVICE_LABEL} | Your device's unique label |
{VARIABLE_LABEL} | The variable you want to monitor |
/lv | Last-value suffix |
If the device or variable doesn't exist, the subscription will fail.
Connection parameters
| Field | Value | Required |
|---|---|---|
| Host | api.surfact.com | Yes |
| Port | 1883 (no TLS) / 8883 (TLS) | Yes |
| Username | Your Surfact token | Yes |
| Password | Any character or blank | No |
| Quality of Service | 0 or 1 | No |
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 1Output:
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:
- Immediate delivery — you receive the variable's current last value right away.
- Updates — you receive new values whenever the variable is updated.
- 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.
Updated 3 months ago
Did this page help you?