fix: when no solar energy, do not enable consumers
This commit is contained in:
@@ -37,7 +37,7 @@ export class ControlLoopRunner {
|
||||
const quarterHour = computeQuarterHourStatus(samples, nowMs, targetW);
|
||||
const current: CurrentReadings = {
|
||||
powerW: latestPower?.watts ?? null,
|
||||
solarW: latestSolar?.watts ?? null,
|
||||
solarW: this.env.SOLAR_INFLUX_ENTITY_ID ? (latestSolar?.watts ?? 0) : null,
|
||||
};
|
||||
|
||||
return { quarterHour, devices, current };
|
||||
@@ -56,17 +56,27 @@ export class ControlLoopRunner {
|
||||
/**
|
||||
* Whether the solar inverter is currently producing power, used to gate
|
||||
* `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
|
||||
* 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> {
|
||||
if (!this.env.SOLAR_INFLUX_ENTITY_ID) {
|
||||
return this.fallbackToSunUp();
|
||||
}
|
||||
try {
|
||||
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) {
|
||||
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) => {
|
||||
this.log.error({ err }, "failed to read sun.sun state; treating as unrestricted");
|
||||
return true;
|
||||
|
||||
@@ -41,7 +41,7 @@ export interface QuarterHourStatus {
|
||||
|
||||
export interface CurrentReadings {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user