Last active
November 23, 2022 20:17
-
-
Save hacki11/9e0831d5726e0d80c9ed3b14c0a0b6e3 to your computer and use it in GitHub Desktop.
Calculate exponential moving average of a temperature sensor and calculate heating curve
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EMAID = 'javascript.0.variables.Jeisha_AT_EMA' | |
const Outside_Temp = 'mqtt.0.heishamon.main.Outside_Temp'; | |
// EMA 1 day, one measurement every minute 24*60 | |
const alpha = 2.0/(24*60-1); | |
function calcExponentialMovingAverage(accumulator, new_value, alpha) { | |
const new_avg = (alpha * new_value) + (1.0 - alpha) * accumulator; | |
// round 2 decimals | |
return Math.round(new_avg * 100)/100; | |
}; | |
function handleEma(emaid, valueid) { | |
// get current ema value | |
const ema = getState(emaid).val | |
// get new measurement | |
const new_value = getState(valueid).val | |
// calculate new ema value | |
const new_ema = calcExponentialMovingAverage(ema, new_value, alpha); | |
// set new ema value | |
setState(emaid, new_ema, true); | |
} | |
// every minute | |
schedule("* * * * *", function () { | |
handleEma(EMAID, Outside_Temp); | |
}); | |
// calculate heating curve | |
const HCID = "javascript.0.variables.Jeisha_VL_CALC"; | |
on({id: EMAID, change: 'any'}, (obj) => { | |
const new_hc = -0.33 * obj.state.val + 28; | |
const new_hc_r = Math.round(new_hc); | |
setState(HCID, new_hc_r, false); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment