@visionmaster
Hi,
please replace the node_helper.js in the module MMM-ShellyPV with the following code. If you receive an error, please describe what you see or what error messages you get. Since I do not have such a device yet it is not possible for me to test it.
At first I start with the Shelly 2.5 device. The “shellyrgbw2” is then the next step!
The config must be like this for these specific 2.5 devices:
shellysPV: [
{ name: "Kueche 1", id: "dcda0cb93bb8", ch: "1" }, // Channel 1 (relay[0])
{ name: "Kueche 2", id: "dcda0cb93bb8", ch: "2" } // Channel 2 (relay[1])
]
I’ve used 1 and 2 instead of 0 and 1. It is easier for my head :-)
And next the updated node_helper.js file:
const NodeHelper = require("node_helper");
const axios = require("axios");
module.exports = NodeHelper.create({
start: function () {
this.config = {};
setTimeout(() => {
console.log("MagicMirror is ready. Fetching ShellyPV status...");
this.fetchShellyPVStatus();
}, 15000); // Delay to ensure MagicMirror is fully loaded
},
socketNotificationReceived: function (notification, payload) {
if (notification === "CONFIG") {
this.config = payload;
if (this.config.shellysPV && Array.isArray(this.config.shellysPV)) {
console.log("Configuration received, fetching ShellyPV status...");
this.fetchShellyPVStatus();
}
} else if (notification === "GET_SHELLYPV_STATUS") {
this.fetchShellyPVStatus();
}
},
fetchShellyPVStatus: async function () {
const results = [];
if (!this.config.shellysPV || !Array.isArray(this.config.shellysPV)) {
console.error("No valid shellysPV configuration found or 'shellysPV' is not an array.");
return;
}
for (const shellyPV of this.config.shellysPV) {
try {
const response = await axios.post(
`${this.config.serverUri}/device/status`,
`id=${shellyPV.id}&auth_key=${this.config.authKey}`
);
const data = response.data?.data?.device_status;
if (data) {
let isOn = false;
let power = null;
// Handle multi-channel devices
if (data.relays && shellyPV.ch) {
const channelIndex = parseInt(shellyPV.ch, 10) - 1; // Convert to zero-based index
if (channelIndex >= 0 && channelIndex < data.relays.length) {
const relay = data.relays[channelIndex];
const meter = data.meters?.[channelIndex];
isOn = relay.ison;
power = meter?.power || 0;
} else {
console.error(`Invalid channel index for ${shellyPV.name}: ${shellyPV.ch}`);
}
}
// Handle single-channel devices (or default behavior for Gen 1/2)
else if (data.relays) {
isOn = data.relays[0].ison;
power = data.meters ? data.meters[0].power : null;
}
// Handle Gen 3 devices (pm1:0 structure)
else if (data["pm1:0"]) {
isOn = true; // Assume true if data exists
power = data["pm1:0"].apower;
}
// Handle switch:0 structure
else if (data["switch:0"]) {
isOn = data["switch:0"].output;
power = data["switch:0"].apower;
}
results.push({
name: shellyPV.name,
isOn: isOn,
power: power !== undefined ? power : null,
statusClass: isOn ? 'on' : 'off', // Dynamically set status class
});
} else {
results.push({
name: shellyPV.name,
isOn: false,
power: null,
statusClass: 'off', // Default to off if no data found
});
}
} catch (error) {
console.error(`Error fetching status for ${shellyPV.name}:`, error);
results.push({
name: shellyPV.name,
isOn: false,
power: null,
statusClass: 'off', // Default to off on error
});
}
}
this.sendSocketNotification("SHELLYPV_STATUS_UPDATE", results);
},
});
For devices without a ch parameter, the logic defaults to using the first relay and meter (relays[0], meters[0]), ensuring compatibility with single-channel or older devices.
If a ch parameter is specified in the config, the code now accesses the corresponding relays and meters arrays for the specific channel.