smartmeter2mqtt
Version:
Publish data from your Smartmeter with a P1 interface to your MQTT server.
212 lines • 10.8 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var mqtt_1 = __importDefault(require("mqtt"));
var output_1 = __importDefault(require("./output"));
var p1_reader_events_1 = __importDefault(require("../p1-reader-events"));
var MqttOutput = /** @class */ (function (_super) {
__extends(MqttOutput, _super);
function MqttOutput(config) {
var _this = _super.call(this) || this;
_this.config = config;
_this.discoverySend = false;
return _this;
}
MqttOutput.prototype.start = function (p1Reader) {
var _this = this;
this.mqtt = mqtt_1.default.connect(this.config.url, this.MqttOptions);
this.mqtt.on('connect', function () {
var _a;
(_a = _this.mqtt) === null || _a === void 0 ? void 0 : _a.publish(_this.config.prefix + "/connected", '2', { qos: 0, retain: true });
});
p1Reader.on(p1_reader_events_1.default.ParsedResult, function (data) {
_this.publishData(data);
if (_this.config.discovery && !_this.discoverySend) {
_this.publishAutoDiscovery(data);
_this.discoverySend = true;
}
});
p1Reader.on(p1_reader_events_1.default.UsageChanged, function (data) {
_this.publishUsage(data);
});
p1Reader.on(p1_reader_events_1.default.GasUsageChanged, function (data) {
_this.publishGasUsage(data);
});
p1Reader.on(p1_reader_events_1.default.SolarResult, function (data) {
_this.publishSolar(data);
});
};
MqttOutput.prototype.close = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve) {
var _a;
(_a = _this.mqtt) === null || _a === void 0 ? void 0 : _a.end(false, {}, resolve);
})];
});
});
};
Object.defineProperty(MqttOutput.prototype, "MqttOptions", {
get: function () {
return {
will: {
topic: this.config.prefix + "/connected",
retain: true,
payload: '0',
qos: 0,
},
};
},
enumerable: true,
configurable: true
});
MqttOutput.prototype.getTopic = function (suffix) {
return this.config.prefix + "/status/" + suffix;
};
MqttOutput.prototype.publishUsage = function (data) {
var _a;
var message = data;
message.val = data.currentUsage;
delete message.currentUsage;
message.tc = Date.now();
(_a = this.mqtt) === null || _a === void 0 ? void 0 : _a.publish(this.getTopic('usage'), JSON.stringify(message), { qos: 0, retain: false });
};
MqttOutput.prototype.publishGasUsage = function (data) {
var _a;
var message = data;
message.val = data.currentUsage;
delete message.currentUsage;
message.tc = Date.now();
(_a = this.mqtt) === null || _a === void 0 ? void 0 : _a.publish(this.getTopic('gasUsage'), JSON.stringify(message), { qos: 0, retain: false });
};
MqttOutput.prototype.publishData = function (data) {
var _this = this;
if (this.config.distinct) {
this.config.distinctFields.forEach(function (element) {
if (data[element]) {
var distinctVal = { ts: Date.now(), val: data[element] };
_this.sendToMqtt(element, distinctVal);
}
});
}
else {
this.sendToMqtt('energy', data);
}
};
MqttOutput.prototype.publishSolar = function (data) {
this.sendToMqtt('solar', data);
};
MqttOutput.prototype.sendToMqtt = function (topicSuffix, data) {
var _a;
(_a = this.mqtt) === null || _a === void 0 ? void 0 : _a.publish(this.getTopic(topicSuffix), JSON.stringify(data), { qos: 0, retain: true });
};
MqttOutput.prototype.publishAutoDiscovery = function (data) {
var _a, _b, _c, _d, _e, _f;
// Current usage
var device = {
// json_attributes: true,
device_class: 'power',
// schema: 'json',
// json_attributes_topic: `${this._options.topic}/status/enegry`,
state_topic: this.config.prefix + "/status/energy",
availability_topic: this.config.prefix + "/connected",
payload_available: '2',
name: 'Current power usage',
icon: 'mdi:speedometer',
unit_of_measurement: 'Watt',
value_template: '{{value_json.calculatedUsage}}',
unique_id: "smartmeter_" + data.powerSn + "_current-usage",
};
(_a = this.mqtt) === null || _a === void 0 ? void 0 : _a.publish(this.config.discoveryPrefix + "/sensor/smartmeter/power-usage/config", JSON.stringify(device), { qos: 0, retain: true });
delete device.icon;
// Total T1
device.unique_id = "smartmeter_" + data.powerSn + "_total_t1_used";
device.unit_of_measurement = 'kWh';
device.value_template = '{{value_json.totalT1Use}}';
device.name = 'Total power used T1';
(_b = this.mqtt) === null || _b === void 0 ? void 0 : _b.publish(this.config.discoveryPrefix + "/sensor/smartmeter/t1-used/config", JSON.stringify(device), { qos: 0, retain: true });
// Total T2
device.unique_id = "smartmeter_" + data.powerSn + "_total_t2_used";
device.unit_of_measurement = 'kWh';
device.value_template = '{{value_json.totalT2Use}}';
device.name = 'Total power used T2';
(_c = this.mqtt) === null || _c === void 0 ? void 0 : _c.publish(this.config.discoveryPrefix + "/sensor/smartmeter/t2-used/config", JSON.stringify(device), { qos: 0, retain: true });
// Total T1 delivered
device.unique_id = "smartmeter_" + data.powerSn + "_total_t1_delivered";
device.unit_of_measurement = 'kWh';
device.value_template = '{{value_json.totalT1Delivered}}';
device.name = 'Total power delivered T1';
(_d = this.mqtt) === null || _d === void 0 ? void 0 : _d.publish(this.config.discoveryPrefix + "/sensor/smartmeter/t1-delivered/config", JSON.stringify(device), { qos: 0, retain: true });
// Total T1 delivered
device.unique_id = "smartmeter_" + data.powerSn + "_total_t2_delivered";
device.unit_of_measurement = 'kWh';
device.value_template = '{{value_json.totalT2Delivered}}';
device.name = 'Total power delivered T2';
(_e = this.mqtt) === null || _e === void 0 ? void 0 : _e.publish(this.config.discoveryPrefix + "/sensor/smartmeter/t2-delivered/config", JSON.stringify(device), { qos: 0, retain: true });
// Total Gas used
if (data.gasSn) {
device.unique_id = "smartmeter_" + data.gasSn + "_total_gas";
device.unit_of_measurement = 'm³';
device.value_template = '{{value_json.gas.totalUse}}';
device.name = 'Total gas usage';
device.icon = 'mdi:gas-cylinder';
delete device.device_class;
(_f = this.mqtt) === null || _f === void 0 ? void 0 : _f.publish(this.config.discoveryPrefix + "/sensor/smartmeter/gas/config", JSON.stringify(device), { qos: 0, retain: true });
}
};
return MqttOutput;
}(output_1.default));
exports.default = MqttOutput;
//# sourceMappingURL=mqtt-output.js.map