Skip to content

Commit

Permalink
chore: patch temperatures with data from HA
Browse files Browse the repository at this point in the history
  • Loading branch information
mdvanes committed May 22, 2024
1 parent cc34a44 commit 8d61cc8
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions apps/server/src/energyusage/energyusage.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
GetDomoticzJsonExport,
GetDomoticzUsePerDayResponse,
GetHaSensorHistoryResponse,
GetHaStatesResponse,
GotGasUsageResponse,
GotTempResponse,
} from "@homeremote/types";
Expand Down Expand Up @@ -71,20 +72,31 @@ const sensorResultsToAggregated =
(
gasEntries: GasUsageItem[],
temperatureSensors: SensorConfig[],
temperatureResponses: GotTempResponse[]
temperatureResponses: GotTempResponse[],
haTemperatureResponses: GetHaSensorHistoryResponse
) =>
(date: string) => {
const temperatureEntries = temperatureResponses.map(
temperatureResponseToEntry(temperatureSensors, date)
);
const gasEntry = gasEntries.find((r) => r.d === date);

// Patch inside temperature with firstHaTemperatureResponse
const x = haTemperatureResponses[0].find(
(entry) => entry.last_changed?.slice(0, 10) === date
);
const temp = Object.fromEntries(temperatureEntries);
if (temp["inside"].avg === "") {
temp["inside"].avg = parseInt(x?.state);
}
// End of patch

const result: EnergyUsageGasItem = {
day: date,
counter: gasEntry?.c ?? "",
used: gasEntry?.v ?? "",

temp: Object.fromEntries(temperatureEntries),
temp: temp,
};
return result;
};
Expand Down Expand Up @@ -236,6 +248,33 @@ export class EnergyUsageController {
return `${this.apiConfig.baseUrl}/json.htm?type=graph&sensor=${sensorConfig.type}&idx=${sensorConfig.idx}&range=month`;
}

getFirstHaTemperatureResponse = async () => {
const haTemperaturesIdsResponse = await fetch(
`${this.haApiConfig.baseUrl}/api/states/sensor.temperatures`,
{
headers: {
Authorization: `Bearer ${this.haApiConfig.token}`,
},
}
);
const haTemperaturesIds =
(await haTemperaturesIdsResponse.json()) as GetHaStatesResponse;

const firstHaTemperatureId = haTemperaturesIds.attributes.entity_id[0];
const time = Date.now();
const startOffset = MONTH;
const startTime = new Date(time - startOffset).toISOString();
const endTime = new Date(time).toISOString();
const url = `${this.haApiConfig.baseUrl}/api/history/period/${startTime}?end_time=${endTime}&filter_entity_id=${firstHaTemperatureId}&minimal_response`;
const firstHaTemperatureResponse = await got(url, {
headers: {
Authorization: `Bearer ${this.haApiConfig.token}`,
},
}).json<GetHaSensorHistoryResponse>();

return firstHaTemperatureResponse;
};

@UseGuards(JwtAuthGuard)
@Get("/gas")
async getGas(
Expand All @@ -260,6 +299,9 @@ export class EnergyUsageController {
temperaturePromises
);

const firstHaTemperatureResponse =
await this.getFirstHaTemperatureResponse();

const now = Date.now();
const NR_OF_DAYS = 31;
const lastMonth = new Date(now - 1000 * 60 * 60 * 24 * NR_OF_DAYS);
Expand All @@ -278,7 +320,8 @@ export class EnergyUsageController {
sensorResultsToAggregated(
gasCounterResponse.result,
temperatureSensors,
temperatureResponses
temperatureResponses,
firstHaTemperatureResponse
)
),
};
Expand Down

0 comments on commit 8d61cc8

Please sign in to comment.