Calculated Fields
Calculated Fields tính toán giá trị dẫn xuất từ dữ liệu thiết bị ngay trên platform mà không cần xử lý ở backend hay script ngoài. Kết quả lưu thành telemetry mới hoặc attribute.
Cách hoạt động
[Device gửi: temperature=25, humidity=60]
↓
[Calculated Field: dewPoint = f(temperature, humidity)]
↓
[Lưu telemetry: dewPoint=13.8°C]
Argument Types (nguồn đầu vào)
| Loại | Mô tả |
|---|---|
| Latest Telemetry | Giá trị telemetry mới nhất của entity |
| Attribute | Attribute (server/client/shared) của entity |
| Rolling Window | Tổng hợp lịch sử telemetry (sum, avg, min, max, count...) |
Loại Calculated Field
Simple Expression
Công thức toán học không cần code:
Expression: temperature * 9/5 + 32
Arguments:
temperature: Latest Telemetry "temperature" from THIS entity
Result key: temperature_f
Output: Telemetry
Ví dụ: Điểm sương (dew point)
Expression: temperature - ((100 - humidity) / 5)
Arguments:
temperature: Latest Telemetry "temperature"
humidity: Latest Telemetry "humidity"
Result key: dew_point
Script Function
JavaScript cho logic phức tạp hơn:
// Tính hiệu suất motor
var efficiency = (output_power / input_power) * 100;
return Math.round(efficiency * 10) / 10;// Tình trạng pin (chia theo ngưỡng)
if (battery_voltage >= 3.7) return "FULL";
if (battery_voltage >= 3.5) return "GOOD";
if (battery_voltage >= 3.3) return "LOW";
return "CRITICAL";Rolling Window (cửa sổ thời gian)
Tính toán trên dữ liệu lịch sử:
Argument: energy_consumption
Type: ROLLING (last 1 hour)
Function: SUM ← tổng tiêu thụ 1 giờ qua
Expression: energy_sum / 60 ← trung bình kWh/phút
Result key: avg_power_kw
Các hàm tổng hợp:
| Hàm | Mô tả |
|---|---|
SUM | Tổng |
AVG | Trung bình |
MIN | Nhỏ nhất |
MAX | Lớn nhất |
COUNT | Số điểm dữ liệu |
NONE | Lấy toàn bộ mảng (dùng trong script) |
Output
Kết quả lưu thành:
| Output Type | Mô tả |
|---|---|
| Telemetry | Dữ liệu chuỗi thời gian (lịch sử được lưu) |
| Attribute | Giá trị tĩnh hiện tại |
Debug Mode
Bật debug để xem input/output của mỗi lần tính:
Debug mode: ✓
Tab Events hiển thị:
Timestamp Input args Result
2024-01-15 09:00:00 {temperature: 25, humidity: 60} {dew_point: 13.8}
2024-01-15 09:01:00 {temperature: 26, humidity: 58} {dew_point: 13.9}
Ví dụ thực tế
Tính mật độ không khí (khí tượng)
// Arguments: temperature (°C), humidity (%), pressure (hPa)
var T = temperature + 273.15; // Kelvin
var Rv = 461.5; // Specific gas constant for water vapor
var Rd = 287.05; // Specific gas constant for dry air
var es = 6.1078 * Math.exp(17.27 * temperature / (temperature + 237.3));
var e = (humidity / 100) * es;
var density = (pressure * 100) / (Rd * T) * (1 - 0.378 * e / (pressure * 100 / 9.80665));
return Math.round(density * 1000) / 1000; // kg/m³Chỉ số chất lượng không khí (AQI)
// Tính AQI từ PM2.5
var pm25 = pm25_concentration;
if (pm25 < 12) return Math.round(pm25 / 12 * 50);
if (pm25 < 35.4) return Math.round(50 + (pm25 - 12) / 23.4 * 50);
if (pm25 < 55.4) return Math.round(100 + (pm25 - 35.4) / 20 * 50);
if (pm25 < 150.4) return Math.round(150 + (pm25 - 55.4) / 95 * 50);
return 300;Tiêu hao điện tháng
Argument: energy_total
Type: Rolling (last 30 days)
Function: MAX - MIN ← chênh lệch đầu/cuối tháng
Result: monthly_kwh
Cấu hình từ Device Detail
- Mở Device > Calculated Fields
- Click Add Calculated Field
- Điền cấu hình và lưu
- Platform sẽ tính lại mỗi khi có dữ liệu mới
Import/Export
Calculated Fields > Export → JSON file
Import ← JSON file
Dùng để sao chép cấu hình giữa các thiết bị cùng loại.