var ConfigData = { statesPrefix: '0_userdata.0.ecoflow', RegulationState: "Regulate", BatMax: 99, // Battery charge level to stop charging BatMin: 50, // Battery charge level to start charging SwitchID: "shelly.0.shellyplusplugs#b0b21c19ff40#1.Relay0.Switch" // Switch controlling the charging }; if (typeof ConfigData.email === 'undefined') { try { let tempConfigData = getState("0_userdata.0.ecoflow.Settings.ConfigData").val if (typeof tempConfigData !== 'object' && tempConfigData !== null) { tempConfigData = JSON.parse(tempConfigData) } if (typeof tempConfigData === 'object' && tempConfigData !== null) { if (tempConfigData.email !== undefined) { ConfigData = tempConfigData; //log("wurde geladen als object") } } } catch (error) { log("Konfiguration wurde nicht geladen: " + error.message) } } // Check for solar power and control charging based on battery level and absence of solar power function controlChargingBasedOnSolarPower() { let solarPowerState = getState(ConfigData.statesPrefix + ".totalPV").val; let batteryStateOfCharge = getState(ConfigData.statesPrefix + ".Settings.Tibber.tibberBatSocID").val; // Replace with your actual Battery State of Charge ID let regulate = getState(ConfigData.statesPrefix + "." + ConfigData.RegulationState).val; // Convert to boolean as getState().val might return string regulate = (regulate === 'true' || regulate === true); if (solarPowerState == 0) { // Assuming 0 means no solar power if (!regulate && batteryStateOfCharge < ConfigData.BatMin) { setState(ConfigData.statesPrefix + "." + ConfigData.RegulationState, false); // Disable regulation setState(ConfigData.SwitchID, true); // Turn on charging log("Charging enabled due to low battery and no solar power."); } else if (regulate && batteryStateOfCharge >= ConfigData.BatMax) { setState(ConfigData.statesPrefix + "." + ConfigData.RegulationState, true); // Re-enable regulation setState(ConfigData.SwitchID, false); // Turn off charging log("Charging disabled, battery is full."); } } else { if (!regulate) { setState(ConfigData.statesPrefix + "." + ConfigData.RegulationState, true); // Ensure regulation is enabled if solar power is present log("Solar power detected, ensuring charging is regulated correctly."); } } } // Setup event listener for changes in solar power state on({id: ConfigData.statesPrefix + ".totalPV", change: "ne"}, function () { controlChargingBasedOnSolarPower(); }); // Call the function initially to set up correct state controlChargingBasedOnSolarPower();