// Script to get data from HomeWizard device
// Tested with HomeWizard P1
let CONFIG = {
// Period to fetch data (ms)
updatePeriod: 10000,
// HomeWizard device ip address
deviceIP: "192.168.20.51:10212",
// HomeWizard API address to fetch measuring data
apiPath: "/api/v1/data",
};
let DATA = {
// Timestamp when data is last updated
valuesUpdated: 0,
// Data model for HomeWizard measures
wifi_ssid: null,
wifi_strength: 0,
smr_version: 0,
meter_model: null,
unique_meter_id: null,
active_tariff: 0,
total_energy_import_kwh: 0.0,
total_energy_import_t1_kwh: 0.0,
total_energy_import_t2_kwh: 0.0,
total_energy_import_t3_kwh: 0.0,
total_energy_import_t4_kwh: 0.0,
total_energy_export_kwh: 0.0,
total_energy_export_t1_kwh: 0.0,
total_energy_export_t2_kwh: 0.0,
total_energy_export_t3_kwh: 0.0,
total_energy_export_t4_kwh: 0.0,
active_power_w: 0.0,
active_power_l1_w: 0.0,
active_power_l2_w: 0.0,
active_power_l3_w: 0.0,
active_voltage_v: 0.0,
active_voltage_l1_v: 0.0,
active_voltage_l2_v: 0.0,
active_voltage_l3_v: 0.0,
active_current_a: 0.0,
active_current_l1_a: 0.0,
active_current_l2_a: 0.0,
active_current_l3_a: 0.0,
active_apparent_power_va: 0.0,
active_apparent_power_l1_va: 0.0,
active_apparent_power_l2_va: 0.0,
active_apparent_power_l3_va: 0.0,
active_reactive_power_var: 0.0,
active_reactive_power_l1_var: 0.0,
active_reactive_power_l2_var: 0.0,
active_reactive_power_l3_var: 0.0,
active_power_factor: 0.0,
active_power_factor_l1: 0.0,
active_power_factor_l2: 0.0,
active_power_factor_l3: 0.0,
active_frequency_hz: 0.0,
voltage_sag_l1_count: 0,
voltage_sag_l2_count: 0,
voltage_sag_l3_count: 0,
voltage_swell_l1_count: 0,
voltage_swell_l2_count: 0,
voltage_swell_l3_count: 0,
any_power_fail_count: 0,
long_power_fail_count: 0,
active_power_average_w: 0.0,
monthly_power_peak_w: 0.0,
monthly_power_peak_timestamp: null,
total_gas_m3: 0.0,
gas_timestamp: null,
gas_unique_id: null,
active_liter_lpm: 0.0,
total_liter_m3: 0.0,
external_devices: {},
};
// Functions
// GET-request from device with parsing callback
function getDataFromApi() {
let urlToCall = "http://" + CONFIG.deviceIP + CONFIG.apiPath;
print('Get data-JSON from device. URL: ', urlToCall);
Shelly.call("HTTP.GET", { url: urlToCall, timeout: 5 }, ParseHttpResponse);
}
// Callback function to parse request data
function ParseHttpResponse(res, error_code, error_msg, ud) {
let requestInfo = null;
let dataJson = {};
if (error_code != 0) {
requestInfo = "Request error: ", error_code, error_msg;
} else {
if (res.code === 200) {
dataJson = JSON.parse(res.body);
for (var item in dataJson) {
DATA[item] = dataJson[item];
}
let timestampMillis = Date.now();
DATA.valuesUpdated = Math.floor(timestampMillis / 1000);
requestInfo = 'Device request succesfull.';
} else {
requestInfo = 'Request error. Code: ' + res.code;
}
res.close;
}
print('Device request done. ', requestInfo);
// Do something here after update request
// Format unix timestamp to HH:MM:SS
let updatedTime = new Date(DATA.valuesUpdated * 1000);
let hours = updatedTime.getHours();
let minutes = "0" + updatedTime.getMinutes();
let seconds = "0" + updatedTime.getSeconds();
let formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
print('Data updated: ' + formattedTime);
// Print some values
print('Total power: ' + DATA.active_power_w + 'W, L1: ' + DATA.active_power_l1_w + 'W, L2: ' + DATA.active_power_l2_w + 'W, L3: ' + DATA.active_power_l3_w + 'W');
}
// Initiate timer to run script
mainTimer = Timer.set(CONFIG.updatePeriod, true, getDataFromApi);