If your sensors already publish to an MQTT broker, you do not need to change them. A small program that runs next to the broker can read the messages and send them on to SensorConsole as web requests. Your devices stay as they are, and you gain charts, history and alerts you can reach from anywhere.
This guide uses a Python script. If you prefer a visual tool, Node-RED can do the same with an MQTT input node and an HTTP request node, and Home Assistant can use its REST command integration.
What you need
- An MQTT broker with your sensors publishing to it. Mosquitto on a Raspberry Pi is typical.
- A computer that is always on and can reach the broker. It can be the same machine as the broker.
- Python 3.8 or newer
- A free SensorConsole account with one Generic/DIY push device created for the readings you want to send. Copy its token from the device page.
How the readings are matched
The script expects one number per topic, with the last part of the topic as the name of the reading. For example, these three messages:
greenhouse/temp_c 21.4
greenhouse/humidity 55
greenhouse/soil_pct 38
are collected and sent together as one reading with the values temp_c, humidity and soil_pct. Messages that are not numbers, such as a status of "online", are ignored.
The script sends the latest values once a minute. SensorConsole saves one history point per device every 5 minutes and shows the newest value live, so a minute is more than enough. SensorConsole accepts one reading per device every 10 seconds, so there is no benefit in sending faster, and the delay also folds several topics into one tidy row.
Install the library
pip install paho-mqtt
The script is written for version 2 of paho-mqtt. If you have an older version, upgrade it with pip install --upgrade paho-mqtt.
The script
Save this as mqtt_to_sensorconsole.py. Set your broker address, the topic pattern to listen to and your token.
import json
import threading
import time
import urllib.request
import paho.mqtt.client as mqtt
BROKER_HOST = "localhost"
BROKER_PORT = 1883
TOPIC = "greenhouse/#"
TOKEN = "paste-your-device-token-here"
PUSH_URL = "https://sensorconsole.com/ingest-generic-push.php?token=" + TOKEN
SEND_EVERY_SECONDS = 60
latest = {}
lock = threading.Lock()
def on_connect(client, userdata, flags, reason_code, properties):
print("Connected to the broker:", reason_code)
client.subscribe(TOPIC)
def on_message(client, userdata, msg):
# greenhouse/temp_c with payload 21.4 becomes {"temp_c": 21.4}
name = msg.topic.split("/")[-1]
try:
value = float(msg.payload.decode())
except ValueError:
return
with lock:
latest[name] = value
def send_latest():
with lock:
data = dict(latest)
latest.clear()
if not data:
return
request = urllib.request.Request(
PUSH_URL,
data=json.dumps(data).encode(),
headers={"Content-Type": "application/json"},
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=15) as response:
print("Sent", data, "->", response.status)
except Exception as error:
print("Send failed:", error)
def main():
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
client.on_connect = on_connect
client.on_message = on_message
client.connect(BROKER_HOST, BROKER_PORT)
client.loop_start()
while True:
time.sleep(SEND_EVERY_SECONDS)
send_latest()
if __name__ == "__main__":
main()
If your broker needs a login, add client.username_pw_set("user", "password") on the line before client.connect.
Run it
python3 mqtt_to_sensorconsole.py
After a minute you should see a line starting with Sent and ending in 200. Open the device page on SensorConsole to see the readings.
Keep it running
A script in a terminal stops when you close the terminal. On a Raspberry Pi or another Linux machine, the usual way to keep it running and start it after a reboot is a systemd service. Create /etc/systemd/system/mqtt-to-sensorconsole.service with this content, changing the user and the path to match yours:
[Unit]
Description=Forward MQTT readings to SensorConsole
After=network-online.target
[Service]
User=pi
ExecStart=/usr/bin/python3 /home/pi/mqtt_to_sensorconsole.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
Then run these commands once:
sudo systemctl daemon-reload
sudo systemctl enable --now mqtt-to-sensorconsole
Several devices
Each SensorConsole device has its own token. If you want separate charts for the greenhouse and the shed, create two devices and run two copies of the script with different topics and tokens. Or, in a single script, keep a small table that maps a topic prefix to a token.
If nothing arrives
- Run
mosquitto_sub -t "greenhouse/#" -von the broker machine to confirm that messages are really being published under that topic. - If the script prints
Send failedwith code 401, the token is wrong. - If it prints nothing after a minute, no numeric messages reached the script. Check the topic pattern.