/* * OTGateway 2.x: Wiren Board module (wb-rules) * * Project: https://github.com/Laxilef/OTGateway/ * Wiki: https://github.com/Laxilef/OTGateway/wiki * * Install: /etc/wb-rules-modules/otgateway.js * OTGateway must use metric units. */ // Topics are relative to the device prefix; setTopic makes a control writable. // Supported attributes: min, max, available, and bindings with topic/callback. var controlDefinitions = { heating_enabled: { cell: { title: {en: 'Heating', ru: 'Отопление'}, type: "switch", value: false }, topic: "settings/heating/enabled", setTopic: "settings/heating/enabled/set" }, heating_turbo: { cell: { title: {en: 'Heating, turbo mode', ru: 'Отопление, турбо режим'}, type: "switch", value: false }, topic: "settings/heating/turbo", setTopic: "settings/heating/turbo/set" }, heating_target_temp: { cell: { title: {en: 'Heating, target temp', ru: 'Отопление, целевая темп.'}, type: "range", value: 25, max: 90, min: 5, precision: 0.5, units: "deg C" }, topic: "settings/heating/target", setTopic: "settings/heating/target/set", attributes: { min: "state/master/heating/min_temp", max: "state/master/heating/max_temp" } }, heating_active: { cell: { title: {en: 'Heating state', ru: 'Отопление, активность'}, type: "switch", value: false }, topic: "state/slave/heating/active" }, heating_current_temp: { cell: { title: {en: 'Heating, current temp', ru: 'Отопление, текущая темп.'}, type: "value", value: 0, units: "deg C" }, topic: "state/master/heating/current_temp", threshold: 0.5 }, heating_return_temp: { cell: { title: {en: 'Heating, return temp', ru: 'Отопление, темп. обратки'}, type: "value", value: 0, units: "deg C" }, topic: "state/master/heating/return_temp", threshold: 0.5 }, dhw_enabled: { cell: { title: {en: 'DHW', ru: 'ГВС'}, type: "switch", value: false }, topic: "settings/dhw/enabled", setTopic: "settings/dhw/enabled/set" }, dhw_target_temp: { cell: { title: {en: 'DHW, target temp', ru: 'ГВС, целевая темп.'}, type: "range", value: 25, max: 90, min: 5, precision: 1.0, units: "deg C" }, topic: "settings/dhw/target", setTopic: "settings/dhw/target/set", attributes: { min: "state/master/dhw/min_temp", max: "state/master/dhw/max_temp" } }, dhw_active: { cell: { title: {en: 'DHW state', ru: 'ГВС, активность'}, type: "switch", value: false }, topic: "state/slave/dhw/active" }, dhw_current_temp: { cell: { title: {en: 'DHW, current temp', ru: 'ГВС, текущая темп.'}, type: "value", value: 0, units: "deg C" }, topic: "state/master/dhw/current_temp", threshold: 0.5 }, dhw_return_temp: { cell: { title: {en: 'DHW, return temp', ru: 'ГВС, темп. обратки'}, type: "value", value: 0, units: "deg C" }, topic: "state/master/dhw/return_temp", threshold: 0.5 }, bus_state: { cell: { title: {en: 'Bus state', ru: 'Состояние шины'}, type: "switch", value: false }, topic: "state/slave/connected" }, flame: { cell: { title: {en: 'Flame state', ru: 'Горелка'}, type: "switch", value: false }, topic: "state/slave/flame" }, fault_active: { cell: { title: {en: 'Fault state', ru: 'Ошибка'}, type: "switch", value: false }, topic: "state/slave/fault/active" }, fault_code: { cell: { title: {en: 'Fault code', ru: 'Код ошибки'}, type: "value", value: 0 }, topic: "state/slave/fault/code" }, diag_active: { cell: { title: {en: 'Diag state', ru: 'Диагностика'}, type: "switch", value: false }, topic: "state/slave/diag/active" }, diag_code: { cell: { title: {en: 'Diag code', ru: 'Код диагностики'}, type: "value", value: 0 }, topic: "state/slave/diag/code" }, heating_setpoint_temp: { cell: { title: {en: 'Heating, setpoint temp', ru: 'Отопление, рассчитанная уставка'}, type: "value", value: 0, units: "deg C" }, topic: "state/master/heating/setpoint_temp" }, heating_indoor_temp: { cell: { title: {en: 'Heating, indoor temp', ru: 'Отопление, внутренняя темп.'}, type: "value", value: 0, units: "deg C" }, topic: "state/master/heating/indoor_temp" }, heating_outdoor_temp: { cell: { title: {en: 'Heating, outdoor temp', ru: 'Отопление, наружная темп.'}, type: "value", value: 0, units: "deg C" }, topic: "state/master/heating/outdoor_temp" }, heating_allowed: { cell: { title: {en: 'Heating, allowed by OTGateway', ru: 'Отопление, разрешено OTGateway'}, type: "switch", value: false }, topic: "state/master/heating/enabled" }, dhw_allowed: { cell: { title: {en: 'DHW, allowed by OTGateway', ru: 'ГВС, разрешено OTGateway'}, type: "switch", value: false }, topic: "state/master/dhw/enabled" }, heating_blocking: { cell: { title: {en: 'Heating, blocking', ru: 'Отопление, блокировка'}, type: "switch", value: false }, topic: "state/master/heating/blocking" }, heating_smart_control: { cell: { title: {en: 'Heating, smart control', ru: 'Отопление, умное регулирование'}, type: "switch", value: false }, topic: "state/master/heating/smart_control" }, heating_overheat: { cell: { title: {en: 'Heating, overheat protection', ru: 'Отопление, защита от перегрева'}, type: "switch", value: false }, topic: "state/master/heating/overheat" }, heating_freezing: { cell: { title: {en: 'Heating, freeze protection', ru: 'Отопление, защита от замерзания'}, type: "switch", value: false }, topic: "state/master/heating/freezing" }, dhw_overheat: { cell: { title: {en: 'DHW, overheat protection', ru: 'ГВС, защита от перегрева'}, type: "switch", value: false }, topic: "state/master/dhw/overheat" }, emergency_active: { cell: { title: {en: 'Emergency mode', ru: 'Аварийный режим'}, type: "switch", value: false }, topic: "state/master/emergency/state" }, ext_pump_active: { cell: { title: {en: 'External pump', ru: 'Дополнительный насос'}, type: "switch", value: false }, topic: "state/master/ext_pump/state" }, cascade_input: { cell: { title: {en: 'Cascade control, input', ru: 'Каскадное управление, вход'}, type: "switch", value: false }, topic: "state/master/cascade_control/input" }, cascade_output: { cell: { title: {en: 'Cascade control, output', ru: 'Каскадное управление, выход'}, type: "switch", value: false }, topic: "state/master/cascade_control/output" }, uptime: { cell: { title: {en: 'Uptime', ru: 'Время работы'}, type: "value", value: 0, units: "s" }, topic: "state/master/uptime" }, pid_enabled: { cell: { title: {en: 'PID', ru: 'ПИД'}, type: "switch", value: false }, topic: "settings/pid/enabled", setTopic: "settings/pid/enabled/set" }, pid_p_factor: { cell: { title: {en: 'PID, P factor', ru: 'ПИД, коэффициент P'}, type: "value", value: 2, min: 0, max: 1000, precision: 0.001 }, topic: "settings/pid/p_factor", setTopic: "settings/pid/p_factor/set" }, pid_i_factor: { cell: { title: {en: 'PID, I factor', ru: 'ПИД, коэффициент I'}, type: "value", value: 0.002, min: 0, max: 100, precision: 0.0001 }, topic: "settings/pid/i_factor", setTopic: "settings/pid/i_factor/set" }, pid_d_factor: { cell: { title: {en: 'PID, D factor', ru: 'ПИД, коэффициент D'}, type: "value", value: 0, min: 0, max: 100000, precision: 0.1 }, topic: "settings/pid/d_factor", setTopic: "settings/pid/d_factor/set" }, pid_dt: { cell: { title: {en: 'PID, calculation interval', ru: 'ПИД, интервал расчёта'}, type: "value", value: 300, min: 30, max: 1800, precision: 1, units: "s" }, topic: "settings/pid/dt", setTopic: "settings/pid/dt/set" }, pid_min_temp: { cell: { title: {en: 'PID, minimum output', ru: 'ПИД, нижний предел выхода'}, type: "value", value: 0, min: -99, max: 99, precision: 1, units: "deg C" }, topic: "settings/pid/min_temp", setTopic: "settings/pid/min_temp/set", attributes: { min: { topic: "settings/equitherm/enabled", callback: function(control, value) { if (value === "true") { control.setMin(-99); } else if (value === "false") { control.setMin(0); } } } } }, pid_max_temp: { cell: { title: {en: 'PID, maximum output', ru: 'ПИД, верхний предел выхода'}, type: "value", value: 90, min: 1, max: 99, precision: 1, units: "deg C" }, topic: "settings/pid/max_temp", setTopic: "settings/pid/max_temp/set" }, pid_deadband_enabled: { cell: { title: {en: 'PID, deadband', ru: 'ПИД, зона нечувствительности'}, type: "switch", value: true }, topic: "settings/pid/deadband/enabled", setTopic: "settings/pid/deadband/enabled/set" }, pid_deadband_p_multiplier: { cell: { title: {en: 'PID, deadband P multiplier', ru: 'ПИД, множитель P в зоне нечувствительности'}, type: "range", value: 1, min: 0, max: 1, precision: 0.001 }, topic: "settings/pid/deadband/p_multiplier", setTopic: "settings/pid/deadband/p_multiplier/set" }, pid_deadband_i_multiplier: { cell: { title: {en: 'PID, deadband I multiplier', ru: 'ПИД, множитель I в зоне нечувствительности'}, type: "range", value: 0.05, min: 0, max: 1, precision: 0.001 }, topic: "settings/pid/deadband/i_multiplier", setTopic: "settings/pid/deadband/i_multiplier/set" }, pid_deadband_d_multiplier: { cell: { title: {en: 'PID, deadband D multiplier', ru: 'ПИД, множитель D в зоне нечувствительности'}, type: "range", value: 1, min: 0, max: 1, precision: 0.001 }, topic: "settings/pid/deadband/d_multiplier", setTopic: "settings/pid/deadband/d_multiplier/set" }, pid_deadband_threshold_high: { cell: { title: {en: 'PID, deadband above target', ru: 'ПИД, зона нечувствительности выше цели'}, type: "range", value: 0.5, min: 0, max: 5, precision: 0.01, units: "deg C" }, topic: "settings/pid/deadband/threshold_high", setTopic: "settings/pid/deadband/threshold_high/set" }, pid_deadband_threshold_low: { cell: { title: {en: 'PID, deadband below target', ru: 'ПИД, зона нечувствительности ниже цели'}, type: "range", value: 1, min: 0, max: 5, precision: 0.01, units: "deg C" }, topic: "settings/pid/deadband/threshold_low", setTopic: "settings/pid/deadband/threshold_low/set" }, equitherm_enabled: { cell: { title: {en: 'Equitherm', ru: 'ПЗА'}, type: "switch", value: false }, topic: "settings/equitherm/enabled", setTopic: "settings/equitherm/enabled/set" }, equitherm_slope: { cell: { title: {en: 'Equitherm, slope', ru: 'ПЗА, наклон кривой'}, type: "value", value: 0.7, min: 0.001, max: 10, precision: 0.001 }, topic: "settings/equitherm/slope", setTopic: "settings/equitherm/slope/set" }, equitherm_exponent: { cell: { title: {en: 'Equitherm, exponent', ru: 'ПЗА, экспонента кривой'}, type: "value", value: 1.3, min: 0.2, max: 2, precision: 0.001 }, topic: "settings/equitherm/exponent", setTopic: "settings/equitherm/exponent/set" }, equitherm_shift: { cell: { title: {en: 'Equitherm, shift', ru: 'ПЗА, смещение кривой'}, type: "range", value: 0, min: -15, max: 15, precision: 0.01, units: "deg C" }, topic: "settings/equitherm/shift", setTopic: "settings/equitherm/shift/set" }, equitherm_target_diff_factor: { cell: { title: {en: 'Equitherm, target difference factor', ru: 'ПЗА, коэффициент T'}, type: "value", value: 2, min: 0, max: 10, precision: 0.001 }, topic: "settings/equitherm/target_diff_factor", setTopic: "settings/equitherm/target_diff_factor/set" }, heating_hysteresis_enabled: { cell: { title: {en: 'Heating, hysteresis', ru: 'Отопление, гистерезис'}, type: "switch", value: true }, topic: "settings/heating/hysteresis/enabled", setTopic: "settings/heating/hysteresis/enabled/set" }, heating_hysteresis_value: { cell: { title: {en: 'Heating, hysteresis value', ru: 'Отопление, величина гистерезиса'}, type: "range", value: 0.5, min: 0.05, max: 15, precision: 0.01, units: "deg C" }, topic: "settings/heating/hysteresis/value", setTopic: "settings/heating/hysteresis/value/set" }, heating_max_modulation: { cell: { title: {en: 'Heating, maximum modulation', ru: 'Отопление, максимальная модуляция'}, type: "range", value: 100, min: 1, max: 100, precision: 1, units: "%" }, topic: "settings/heating/max_modulation", setTopic: "settings/heating/max_modulation/set" }, dhw_max_modulation: { cell: { title: {en: 'DHW, maximum modulation', ru: 'ГВС, максимальная модуляция'}, type: "range", value: 100, min: 1, max: 100, precision: 1, units: "%" }, topic: "settings/dhw/max_modulation", setTopic: "settings/dhw/max_modulation/set" } }; function createDevice(config) { var deviceId = config.id; var name = config.name; var prefix = config.prefix; var online = false; var controlState = {}; var definitions = copyObject(controlDefinitions); var attributeSetters = {min: "setMin", max: "setMax"}; var cells = {}; var position = 1; Object.keys(config.sensors || {}).forEach(function(sensorId) { definitions["sensor_" + sensorId] = config.sensors[sensorId]; }); (config.disabledDefinitions || []).forEach(function(controlName) { delete definitions[controlName]; }); Object.keys(definitions).forEach(function(controlName) { var definition = definitions[controlName]; var cell = copyObject(definition.cell); cell.order = position++; cell.readonly = !definition.setTopic; cells[controlName] = cell; }); var instance = defineVirtualDevice(deviceId, { title: name, cells: cells }); Object.keys(definitions).forEach(function(controlName) { var definition = definitions[controlName]; var control = instance.getControl(controlName); var attributes = definition.attributes || {}; var state = {valid: false, available: !attributes.available}; controlState[controlName] = state; // wb-rules ignores units in range definitions during creation. if (definition.cell.type === "range" && definition.cell.units) { control.setUnits(definition.cell.units); } control.setError("r"); trackMqtt(prefix + "/" + definition.topic, function(message) { var value = parseControlValue(control, message.value); var hadValue = state.valid; state.valid = value !== null; if (state.valid) { // Remember the received value before normal WB notifications run. state.value = value; if (definition.threshold && hadValue) { updateNumberValue(control, value, definition.threshold); } else { control.setValue(value); } } updateControlError(controlName); }); Object.keys(attributes).forEach(function(attribute) { var binding = attributes[attribute]; var topic = typeof binding === "string" ? binding : binding.topic; trackMqtt(prefix + "/" + topic, function(message) { if (typeof binding.callback === "function") { binding.callback(control, message.value); return; } if (attribute === "available") { state.available = message.value === "true"; updateControlError(controlName); return; } var value = parseControlValue(control, message.value); if (value !== null) { // Metadata must not replace the value used for command suppression. control[attributeSetters[attribute]](value); } }); }); }); trackMqtt(prefix + "/status", function(message) { online = message.value === "online"; Object.keys(definitions).forEach(updateControlError); }); var writableControls = Object.keys(definitions).filter(function(controlName) { return !!definitions[controlName].setTopic; }); defineRule({ whenChanged: writableControls.map(function(controlName) { return deviceId + "/" + controlName; }), then: function(newValue, deviceId, controlName) { var definition = definitions[controlName]; var control = instance.getControl(controlName); var state = controlState[controlName]; if (!definition.setTopic || control.getReadonly()) { return; } newValue = parseControlValue(control, newValue); if (newValue === null) { return; } // Ignore queued older events and the last received or sent value. if (newValue !== parseControlValue(control, control.getValue()) || newValue === state.value) { return; } // Remember pending commands so a quick change back is still sent. state.value = newValue; publish(prefix + "/" + definition.setTopic, String(newValue), 0, false); } }); function updateControlError(controlName) { var state = controlState[controlName]; var available = online && state.valid && state.available; instance.getControl(controlName).setError(available ? "" : "r"); } return instance; } function copyObject(source) { var result = {}; Object.keys(source).forEach(function(key) { result[key] = source[key]; }); return result; } function parseControlValue(control, value) { if (control.getType() === "switch") { if (value === true || value === "true" || value === 1 || value === "1") { return true; } if (value === false || value === "false" || value === 0 || value === "0") { return false; } return null; } if (typeof value === "string") { if (value.trim() === "") { return null; } value = Number(value); } return typeof value === "number" && isFinite(value) ? value : null; } function updateNumberValue(control, value, threshold) { var previous = parseControlValue(control, control.getValue()); if (previous === null || Math.abs(previous - value) >= threshold) { control.setValue(value); } } exports.defaultSensors = { "outdoor_temp": { cell: { title: {en: "Outdoor temp", ru: "Наружная температура"}, type: "value", value: 0, units: "deg C" }, topic: "sensors/outdoor_temp/value", attributes: { available: "sensors/outdoor_temp/connected" } }, "indoor_temp": { cell: { title: {en: "Indoor temp", ru: "Внутренняя температура"}, type: "value", value: 0, units: "deg C" }, topic: "sensors/indoor_temp/value", attributes: { available: "sensors/indoor_temp/connected" } }, "heating_temp": { cell: { title: {en: "Heating temp", ru: "Отопление, подача"}, type: "value", value: 0, units: "deg C" }, topic: "sensors/heating_temp/value", attributes: { available: "sensors/heating_temp/connected" } }, "heating_return_temp": { cell: { title: {en: "Heating return temp", ru: "Отопление, обратка"}, type: "value", value: 0, units: "deg C" }, topic: "sensors/heating_return_temp/value", attributes: { available: "sensors/heating_return_temp/connected" } }, "heating_setpoint_temp": { cell: { title: {en: "Heating setpoint temp", ru: "Отопление, рассчитанная уставка"}, type: "value", value: 0, units: "deg C" }, topic: "sensors/heating_setpoint_temp/value", attributes: { available: "sensors/heating_setpoint_temp/connected" } }, "dhw_temp": { cell: { title: {en: "DHW temp", ru: "ГВС, температура"}, type: "value", value: 0, units: "deg C" }, topic: "sensors/dhw_temp/value", attributes: { available: "sensors/dhw_temp/connected" } }, "dhw_flow_rate": { cell: { title: {en: "DHW flow rate", ru: "ГВС, расход"}, type: "value", value: 0, units: "l/min" }, topic: "sensors/dhw_flow_rate/value", attributes: { available: "sensors/dhw_flow_rate/connected" } }, "exhaust_temp": { cell: { title: {en: "Exhaust temp", ru: "Темп. выхлопа"}, type: "value", value: 0, units: "deg C" }, topic: "sensors/exhaust_temp/value", attributes: { available: "sensors/exhaust_temp/connected" } }, "pressure": { cell: { title: {en: "Pressure", ru: "Давление"}, type: "value", value: 0, units: "bar" }, topic: "sensors/pressure/value", attributes: { available: "sensors/pressure/connected" } }, "modulation_level": { cell: { title: {en: "Modulation level", ru: "Модуляция"}, type: "value", value: 0, units: "%" }, topic: "sensors/modulation_level/value", attributes: { available: "sensors/modulation_level/connected" } }, "power": { cell: { title: {en: "Power", ru: "Мощность"}, type: "value", value: 0, units: "kW" }, topic: "sensors/power/value", attributes: { available: "sensors/power/connected" } } }; // Presets exclude WB controls without changing OTGateway settings. exports.definitionPresets = { no_dhw: [ "dhw_enabled", "dhw_target_temp", "dhw_active", "dhw_current_temp", "dhw_return_temp", "dhw_allowed", "dhw_overheat", "dhw_max_modulation", "sensor_dhw_temp", "sensor_dhw_flow_rate" ], no_equitherm: [ "equitherm_enabled", "equitherm_slope", "equitherm_exponent", "equitherm_shift", "equitherm_target_diff_factor" ], no_pid: [ "pid_enabled", "pid_p_factor", "pid_i_factor", "pid_d_factor", "pid_dt", "pid_min_temp", "pid_max_temp", "pid_deadband_enabled", "pid_deadband_p_multiplier", "pid_deadband_i_multiplier", "pid_deadband_d_multiplier", "pid_deadband_threshold_high", "pid_deadband_threshold_low" ], no_hysteresis: [ "heating_hysteresis_enabled", "heating_hysteresis_value" ], no_extpump: [ "ext_pump_active" ], no_cascade: [ "cascade_input", "cascade_output" ], no_internal_state: [ "heating_current_temp", "heating_return_temp", "dhw_current_temp", "dhw_return_temp", "heating_setpoint_temp", "heating_indoor_temp", "heating_outdoor_temp", "heating_allowed", "dhw_allowed", "heating_blocking", "heating_smart_control", "uptime" ] }; exports.definitionPresets.direct_control = [].concat( exports.definitionPresets.no_internal_state, exports.definitionPresets.no_pid, exports.definitionPresets.no_equitherm, exports.definitionPresets.no_hysteresis, [ "heating_turbo", "emergency_active", "sensor_heating_setpoint_temp" ] ); // Native OTC still uses hysteresis and emergency mode. exports.definitionPresets.native_otc = [].concat( exports.definitionPresets.no_internal_state, exports.definitionPresets.no_pid, exports.definitionPresets.no_equitherm, [ "heating_turbo", "sensor_heating_setpoint_temp" ] ); exports.createDevice = createDevice;