Skip to content

Commit

Permalink
Add option to MQTT for retain flag
Browse files Browse the repository at this point in the history
  • Loading branch information
bkbilly committed Mar 21, 2020
1 parent 2090a56 commit 3a7b2e8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,24 @@ As of 1.2.0 the Hassio addon supports multiple devices by running paralell NodeJ
config example:

```json
"mqtt": {
"broker_url": "mqtt:https://localhost",
},
"devices": [
{
"hvac_host": "192.168.0.255",
"mqtt_topic_prefix": "/home/hvac01"
},
{
"hvac_host": "192.168.0.254",
"mqtt_topic_prefix": "/home/hvac02"
}
]
{
"mqtt": {
"broker_url": "mqtt:https://localhost",
"username": "user",
"password": "pass",
"retain": false
},
"devices": [
{
"hvac_host": "192.168.0.255",
"mqtt_topic_prefix": "/home/hvac01"
},
{
"hvac_host": "192.168.0.254",
"mqtt_topic_prefix": "/home/hvac02"
}
]
}
```

## Configuring HVAC WiFi
Expand All @@ -175,6 +180,7 @@ Note: This command may vary depending on your OS (e.g. Linux, macOS, CygWin). If

- Fix run script for single device with same configuration
- Run single device as a systemd service
- Add option to MQTT for retain flag

[1.2.2]

Expand Down
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ const deviceState = {
* @param {string} mqttTopic Topic (without prefix) to send with new value
*/
const publishIfChanged = function (stateProp, newValue, mqttTopic) {
const pubmqttOptions = {
retain: false
}
if (argv['mqtt-retain']) {
pubmqttOptions.retain = (argv['mqtt-retain'] == "true")
}
if (newValue !== deviceState[stateProp]) {
deviceState[stateProp] = newValue
client.publish(mqttTopicPrefix + mqttTopic, newValue)
client.publish(mqttTopicPrefix + mqttTopic, newValue, pubmqttOptions)
}
}

Expand Down
11 changes: 9 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ MQTT_BROKER_URL=$(jq -r ".mqtt.broker_url" $CONFIG_PATH)
MQTT_TOPIC_PREFIX=$(jq -r ".mqtt.topic_prefix" $CONFIG_PATH)
MQTT_USERNAME=$(jq -r ".mqtt.username" $CONFIG_PATH)
MQTT_PASSWORD=$(jq -r ".mqtt.password" $CONFIG_PATH)
MQTT_RETAIN=$(jq -r ".mqtt.retain" $CONFIG_PATH)
if [ "$MQTT_RETAIN" = null ]; then
MQTT_RETAIN=false
fi

echo "MQTT_RETAIN: ${MQTT_RETAIN}"
npm install

INSTANCES=$(jq '.devices | length' $CONFIG_PATH)
Expand All @@ -25,7 +30,8 @@ if [ "$INSTANCES" -gt 1 ]; then
--mqtt-broker-url="${MQTT_BROKER_URL}" \
--mqtt-topic-prefix="${MQTT_TOPIC_PREFIX}" \
--mqtt-username="${MQTT_USERNAME}" \
--mqtt-password="${MQTT_PASSWORD}"
--mqtt-password="${MQTT_PASSWORD}" \
--mqtt-retain="${MQTT_RETAIN}"
done
npx pm2 logs /HVAC_/
else
Expand All @@ -38,5 +44,6 @@ else
--mqtt-broker-url="${MQTT_BROKER_URL}" \
--mqtt-topic-prefix="${MQTT_TOPIC_PREFIX}" \
--mqtt-username="${MQTT_USERNAME}" \
--mqtt-password="${MQTT_PASSWORD}"
--mqtt-password="${MQTT_PASSWORD}" \
--mqtt-retain="${MQTT_RETAIN}"
fi

0 comments on commit 3a7b2e8

Please sign in to comment.