基于日期/时间调度操作的ThingsBoard单服务器场景技术问询
Got it, let's walk through exactly how to set up date/time-based scheduling for your device_name device in your single-server ThingsBoard instance. Since your device is already connected via MQTT using token device_token, and you've got the read_sensor method linked to a working dashboard, we've got three solid approaches to choose from—starting with the most native ThingsBoard solution:
This is the cleanest, platform-native way to schedule actions without external tools. We'll set up a rule chain to trigger the read_sensor RPC method on your device at specified times:
- Navigate to Rule Engine in the ThingsBoard UI, then either create a new rule chain or use the default root chain.
- Add a Timer Node to your chain:
- Choose your trigger type: pick CRON for precise date/time scheduling (e.g., daily at 8 AM) or Period for interval-based triggers (e.g., every 30 minutes).
- For CRON, use the standard format
seconds minutes hours day month weekday year(year is optional). Example:0 0 8 * * ?triggers exactly at 8:00 AM every day.
- Add an RPC Call Node to the chain:
- Under Target Devices, select
device_name(or use a filter if you want to scale later). - Set the Method Name to
read_sensor, and leave Parameters empty if your method doesn't require inputs.
- Under Target Devices, select
- Connect the Success output of the Timer Node to the input of the RPC Call Node.
- Save the rule chain and toggle it to Active.
Every time the timer triggers, ThingsBoard will send an RPC request to your device, triggering read_sensor and pulling the sensor data into your dashboard automatically.
If you need conditional scheduling (e.g., trigger only on the last day of the month, or during specific date ranges), combine a basic timer with a script node to validate the time:
- Add a Timer Node set to a frequent interval (e.g., every minute) as a base trigger.
- Add a Script Node right after it, with JavaScript logic to check if the current time meets your criteria. Example:
const now = new Date(); // Trigger only on the last day of the month at 10:00 AM const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate(); if (now.getDate() === lastDayOfMonth && now.getHours() === 10 && now.getMinutes() === 0) { // Pass the message through to trigger the RPC call return { msg: msg, metadata: metadata, msgType: msgType }; } else { // Drop the message if conditions aren't met return null; } - Connect the Success output of the Script Node to your existing RPC Call Node (from Method 1).
- Save and activate the chain.
This lets you handle custom scheduling logic that CRON alone can't easily manage.
If you prefer using external tools (like Linux cron or Windows Task Scheduler), you can write a simple script to send an MQTT RPC request directly to ThingsBoard on a schedule. Here's a Python example using the paho-mqtt library:
import paho.mqtt.client as mqtt import json # Configure your ThingsBoard details THINGSBOARD_HOST = "your-thingsboard-ip" DEVICE_TOKEN = "device_token" # RPC topic format: v1/devices/me/rpc/request/{request-id} RPC_TOPIC = "v1/devices/me/rpc/request/1" def on_connect(client, userdata, flags, rc): print(f"Connected to ThingsBoard with code {rc}") # Send RPC request to trigger read_sensor rpc_payload = {"method": "read_sensor", "params": {}} client.publish(RPC_TOPIC, json.dumps(rpc_payload)) # Disconnect after sending client.disconnect() client = mqtt.Client() client.username_pw_set(DEVICE_TOKEN) client.on_connect = on_connect client.connect(THINGSBOARD_HOST, 1883, 60) client.loop_forever()
Then schedule this script with cron (Linux):
# Run every day at 9 AM 0 9 * * * /usr/bin/python3 /path/to/your/sensor_trigger.py
Key Notes
- Ensure your rule chain has permissions to access
device_name(check the Permissions tab in the rule chain settings). - For RPC calls, if your device is offline, ThingsBoard can cache the request (configure this in the RPC Call Node's advanced settings).
- Test your CRON expressions first to make sure they trigger at the right time—you can use the preview in the Timer Node to verify.
内容的提问来源于stack exchange,提问作者Flyingfenix




