Skip to content

Commit

Permalink
Better rounding functions for temp conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTerBeke committed Apr 24, 2024
1 parent 57e9100 commit c6e7f13
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/UponorHTTPClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export class UponorHTTPClient {
}

public async setTargetTemperature(controllerID: number, thermostatID: number, value: number): Promise<void> {
const setPoint = Math.round(((value * 9 / 5) + 32) * 10).toString()
const fahrenheit = (value * 9 / 5) + 32
const setPoint = round(fahrenheit * 10, 0).toString()
await this._setAttribute(`C${controllerID}_T${thermostatID}_setpoint`, setPoint)
}

Expand All @@ -128,16 +129,20 @@ export class UponorHTTPClient {
if (data.result != 'OK') {
return Promise.reject(data.result)
}

await this._syncAttributes()
}

private static _formatTemperature(input: string | undefined): number {
const fahrenheit = parseFloat(input || '0') / 10
return Math.round((fahrenheit - 32) * 5 / 9)
const celcius = (fahrenheit - 32) * 5 / 9
return round(celcius, 1)
}

private static _createKey(controllerID: string | number, thermostatID: string | number): string {
return `C${controllerID}_T${thermostatID}`
}
}

function round(value: number, precision: number = 0): number {
var multiplier = Math.pow(10, precision)
return Math.round(value * multiplier) / multiplier
}

0 comments on commit c6e7f13

Please sign in to comment.