Skip to content

Instantly share code, notes, and snippets.

@sroebert
Last active February 6, 2022 10:47
Show Gist options
  • Save sroebert/59ea06ce8b7b89f597224bc73d30f3da to your computer and use it in GitHub Desktop.
Save sroebert/59ea06ce8b7b89f597224bc73d30f3da to your computer and use it in GitHub Desktop.
shelly-plus-1-mqtt-script
function announce(topicPrefix) {
MQTT.publish(topicPrefix + "/online", "true")
Shelly.call(
"Sys.GetConfig",
{},
function(systemConfig, errorCode, errorMessage, data) {
if (systemConfig.device !== undefined) {
data.payload.id = "shellyplus1-" + systemConfig.device.mac
data.payload.mac = systemConfig.device.mac
data.payload.fw_ver = systemConfig.device.fw_id
data.payload.new_fw = false
}
Shelly.call(
"Wifi.GetStatus",
{},
function(wifiStatus, errorCode, errorMessage, data) {
if (wifiStatus.sta_ip !== undefined) {
data.payload.ip = wifiStatus.sta_ip
}
MQTT.publish(data.topicPrefix + "/status", JSON.stringify(data.payload))
},
data
)
},
{
topicPrefix: topicPrefix,
payload: {}
}
)
Shelly.call(
"Input.GetStatus",
{ id: 0 },
function(status, errorCode, errorMessage, topicPrefix) {
MQTT.publish(topicPrefix + "/status/input:0", JSON.stringify(status))
},
topicPrefix
)
Shelly.call(
"Switch.GetStatus",
{ id: 0 },
function(status, errorCode, errorMessage, topicPrefix) {
MQTT.publish(topicPrefix + "/status/switch:0", JSON.stringify(status))
},
topicPrefix
)
}
Shelly.call(
"MQTT.GetConfig",
{},
function(config) {
if (config.topic_prefix === undefined || config.topic_prefix === null) {
return
}
MQTT.subscribe(
"shellies/command",
function(topic, message, topicPrefix) {
if (message === "announce") {
announce(topicPrefix)
}
},
config.topic_prefix
)
MQTT.subscribe(
config.topic_prefix + "/command",
function(topic, message, topicPrefix) {
if (message === "announce") {
announce(topicPrefix)
}
},
config.topic_prefix
)
MQTT.subscribe(
config.topic_prefix + "/command/switch:0/output",
function(topic, message, topicPrefix) {
if (message !== "on" && message !== "off") {
print("Invalid switch command received: " + message)
return
}
Shelly.call(
"Switch.Set",
{ id: 0, on: message === "on" }
)
},
config.topic_prefix
)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment