Skip to content

Commit

Permalink
fix: Mqtt auto discovery
Browse files Browse the repository at this point in the history
Instant integration with home assistant.
  • Loading branch information
Stephan van Rooij committed Apr 13, 2020
1 parent 1a87379 commit 14225a0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ class Smartmeter {
this._startTcpServer(config['tcp-server'])
}

if (config['mqtt-url']) this._startMqtt({ url: config['mqtt-url'], topic: config['mqtt-topic'], publishDistinct: config['mqtt-distinct'] === true })
if (config['mqtt-url']) {
this._startMqtt({
url: config['mqtt-url'],
topic: config['mqtt-topic'],
discovery: config['mqtt-discovery'] === true,
discoveryPrefix: config['mqtt-discovery-prefix'],
publishDistinct: config['mqtt-distinct'] === true
})
}

if (config['post-url']) this._startHttp({ url: config['post-url'], interval: config['post-interval'], postJson: config['post-json'] === true })

Expand Down
6 changes: 5 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const config = require('yargs')
.describe('mqtt-topic', 'Use this topic prefix for all messages')
.describe('mqtt-distinct', 'Publish data distinct to mqtt')
.boolean('mqtt-distinct')
.describe('mqtt-discovery', 'Emit auto-discovery message')
.boolean('mqtt-discovery')
.describe('mqtt-discovery-prefix', 'Autodiscovery prefix')
.describe('tcp-server', 'Expose JSON TCP socket on this port')
.describe('raw-tcp-server', 'Expose RAW TCP socket on this port')
.conflicts('port', 'socket')
Expand All @@ -32,7 +35,8 @@ const config = require('yargs')
})
.default({
'post-interval': 300,
'mqtt-topic': 'smartmeter'
'mqtt-topic': 'smartmeter',
'mqtt-discovery-prefix': 'homeassistant'
})
.wrap(80)
.version()
Expand Down
67 changes: 67 additions & 0 deletions lib/output/mqtt-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class MqttOutput extends Output {
super()
this._mqtt = null
this._options = {}
this._discoverySend = false
}

start (p1Reader, options = {}) {
Expand All @@ -17,6 +18,10 @@ class MqttOutput extends Output {
})
p1Reader.on(P1ReaderEvents.ParsedResult, data => {
this._publishData(data)
if (this._options.discovery && !this._discoverySend) {
this._publishAutoDiscovery(data)
this._discoverySend = true
}
})
p1Reader.on(P1ReaderEvents.UsageChanged, data => {
this._publishUsage(data)
Expand Down Expand Up @@ -68,10 +73,72 @@ class MqttOutput extends Output {
this._mqtt.publish(this._getTopic(topicSuffix), JSON.stringify(data), { qos: 0, retain: true })
}

_publishAutoDiscovery (data) {
// Current usage
const device = {
// json_attributes: true,
device_class: 'power',
// schema: 'json',
// json_attributes_topic: `${this._options.topic}/status/enegry`,
state_topic: `${this._options.topic}/status/energy`,
availability_topic: `${this._options.topic}/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`
}
this._mqtt.publish(`${this._options.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'
this._mqtt.publish(`${this._options.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'
this._mqtt.publish(`${this._options.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'
this._mqtt.publish(`${this._options.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'
this._mqtt.publish(`${this._options.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
this._mqtt.publish(`${this._options.discoveryPrefix}/sensor/smartmeter/gas/config`, JSON.stringify(device), { qos: 0, retain: true })
}
}

static get DefaultOptions () {
return {
topic: 'smartmeter',
publishDistinct: false,
discovery: false,
discoveryPrefix: 'homeassistant',
distinctFields: ['currentTarrif', 'totalT1Use', 'totalT2Use', 'totalT1Delivered', 'totalT2Delivered', 'powerSn', 'currentUsage', 'currentDelivery'],
url: 'mqtt://localhost:1883'
}
Expand Down

0 comments on commit 14225a0

Please sign in to comment.