fix: when no solar energy, do not enable consumers

This commit is contained in:
willem.serruys
2026-08-22 19:56:31 +02:00
parent 7bf479f90a
commit ff749940e6
2 changed files with 15 additions and 5 deletions

View File

@@ -37,7 +37,7 @@ export class ControlLoopRunner {
const quarterHour = computeQuarterHourStatus(samples, nowMs, targetW); const quarterHour = computeQuarterHourStatus(samples, nowMs, targetW);
const current: CurrentReadings = { const current: CurrentReadings = {
powerW: latestPower?.watts ?? null, powerW: latestPower?.watts ?? null,
solarW: latestSolar?.watts ?? null, solarW: this.env.SOLAR_INFLUX_ENTITY_ID ? (latestSolar?.watts ?? 0) : null,
}; };
return { quarterHour, devices, current }; return { quarterHour, devices, current };
@@ -56,17 +56,27 @@ export class ControlLoopRunner {
/** /**
* Whether the solar inverter is currently producing power, used to gate * Whether the solar inverter is currently producing power, used to gate
* `onlyWhenSun` devices. Reads the configured InfluxDB solar production * `onlyWhenSun` devices. Reads the configured InfluxDB solar production
* series and compares its latest sample against SOLAR_PRODUCTION_THRESHOLD_W. * series and compares its latest sample (treating no reading within the
* lookback window as 0W) against SOLAR_PRODUCTION_THRESHOLD_W.
* Falls back to HA's sun.sun (astronomical above/below horizon) when no * Falls back to HA's sun.sun (astronomical above/below horizon) when no
* solar production series is configured, or if either read fails. * solar production series is configured at all, or if the Influx read
* itself fails.
*/ */
private async isSolarProducing(): Promise<boolean> { private async isSolarProducing(): Promise<boolean> {
if (!this.env.SOLAR_INFLUX_ENTITY_ID) {
return this.fallbackToSunUp();
}
try { try {
const sample = await fetchLatestSolarProductionSample(this.queryApi, this.env); const sample = await fetchLatestSolarProductionSample(this.queryApi, this.env);
if (sample) return sample.watts >= this.env.SOLAR_PRODUCTION_THRESHOLD_W; const watts = sample?.watts ?? 0;
return watts >= this.env.SOLAR_PRODUCTION_THRESHOLD_W;
} catch (err) { } catch (err) {
this.log.error({ err }, "failed to read solar production series; falling back to sun.sun"); this.log.error({ err }, "failed to read solar production series; falling back to sun.sun");
return this.fallbackToSunUp();
} }
}
private fallbackToSunUp(): Promise<boolean> {
return this.ha.isSunUp().catch((err) => { return this.ha.isSunUp().catch((err) => {
this.log.error({ err }, "failed to read sun.sun state; treating as unrestricted"); this.log.error({ err }, "failed to read sun.sun state; treating as unrestricted");
return true; return true;

View File

@@ -41,7 +41,7 @@ export interface QuarterHourStatus {
export interface CurrentReadings { export interface CurrentReadings {
powerW: number | null; // most recent house power sample, null if none within lookback powerW: number | null; // most recent house power sample, null if none within lookback
solarW: number | null; // most recent solar production sample, null if unconfigured or none within lookback solarW: number | null; // most recent solar production sample; 0 if configured but no reading within lookback, null if unconfigured
} }
export interface ControlAction { export interface ControlAction {