Ich werde dir den Script nicht runter-kriffeln, aber ich habe für dich ein Beispiel erzeugt, welches 2 Variablen(MQTT-Topics) liest und 2 schreibt.
Minimal Einstellungen Shelly:
Beispiel Script:
Code
/// Shelly Script MQTT Example V1.0
////////////////////////////////////////////////////////////////////////////////////////////
///
/// Created by HighFive © 2024
///
////////////////////////////////////////////////////////////////////////////////////////////
/// Begin USER settings
////////////////////////////////////////////////////////////////////////////////////////////
let log = 1; // If NO console log needed please set log to "0"
////////////////////////////////////////////////////////////////////////////////////////////
/// Begin MQTT variables/parameters
////////////////////////////////////////////////////////////////////////////////////////////
let SHELLY_ID = undefined; //
let MQTTpublish = true; // Set this to false to stop publishing on MQTT
let MQTTconnect = false; // default / set in program
// MQTT READ TOPICS
let topicHomeModus = "Home Assistant/Tado-Modus" // "HOME"/"AWAY" (HA dedect handys in home cycle of 10 km
let topicBadebetrieb = "Home Assistant/Prepare bath temperature" // "start" (rising edge trigger start/stop)
////////////////////////////////////////////////////////////////////////////////////////////
/// Prepare MQTT Read topics
////////////////////////////////////////////////////////////////////////////////////////////
// Query the MQTT prefix on startup
Shelly.call("Mqtt.GetConfig", "", function(res, err_code, err_msg, ud) {
MQTT.subscribe(topicHomeModus, MQTTCmdListener);
MQTT.subscribe(topicBadebetrieb, MQTTCmdListener);
SHELLY_ID = res["topic_prefix"];
});
////////////////////////////////////////////////////////////////////////////////////////////
/// Listen on MQTT Read topics changes
////////////////////////////////////////////////////////////////////////////////////////////
function MQTTCmdListener(topic, message) {
if (topic === topicHomeModus) {
if (log != 0) {
print('MQTT topic: ' + topic + ' changed to: ' + message);
}
}
if (topic === topicBadebetrieb) {
if (log != 0) {
print('MQTT topic: ' + topic + ' changed to: ' + message);
}
}
}
///
function timerHandler(user_data) {
MQTTconnect = MQTT.isConnected();
////////////////////////////////////////////////////////////////////////////////////////////
/// MQTT Write topics to MQTT broker
////////////////////////////////////////////////////////////////////////////////////////////
if (typeof SHELLY_ID !== "undefined" && MQTTconnect === true && MQTTpublish === true) {
MQTT.publish(SHELLY_ID + "/Shelly_To_MQTT/Value_1", Math.random().toString(), 0, false);
MQTT.publish(SHELLY_ID + "/Shelly_To_MQTT/Value_2", Math.random().toString(), 0, false);
if (log != 0) {
print('2 MQTT topics write with random values to MQTT broker');
}
}
}
///
Timer.set(3000, true, timerHandler, null);
Alles anzeigen
MQTT: