Just started experimenting with JavaScript yesterday and was given a small task to create a script for a building controller. The task involves reading values from specific locations, performing energy calculations, and then after 1 hour, subtracting the initial values to get the delta energy difference. To keep the script running constantly, I am using a while loop with sleep function.
Below is the code I have developed so far:
function sum() {
var E1 = (parseFloat(read("location1","value1")) *
parseFloat(read("location11","value11"))) / Math.pow(10,6)
executePropertyCommand("object1","Value","Write", E1)
var E2 = (parseFloat(read("location2","value2")) *
parseFloat(read("location22","value22"))) / Math.pow(10,6)
executePropertyCommand("object2","Value","Write", E2)
var IT_Sum = (E1 + E2)
return IT_Sum}
setTimeout(sum1,3.599 * 1000000);{
function sum1() {
var E1 = (parseFloat(read("location1","value1")) *
parseFloat(read("location1","value1"))) / Math.pow(10,6)
var E2 = (parseFloat(read("location2","value2")) *
parseFloat(read("location22","value2"))) / Math.pow(10,6)
var IT_Sum1 = (E1 + E2)
return IT_Sum1 }}
while(1) {
var sum1 = sum()
var sum2 = sum1()
var IT_delta = sum2 - sum1
sleep(3.6 * 1000000)}
I have experimented with different placements of the setTimeout function within the code, but I am struggling to get sum2 to wait for the delay. Any suggestions for a more efficient way to calculate the subtraction of the same data in hourly loops?