Average outside temperature with Lua
Posted: Friday 16 November 2018 19:12
Hi,
I want to measure the average temperature from my outside temperature sensor, this is a sensor that measure, temperature and humidity.
I'm not familiar with the Lua scripts.
But I found an example on the the internet
My IDX from the temperature/humidity sensor is IDX 37 (Buiten Temp)
The dummy temperature has the IDX 247 (Gemiddelde buiten temperatuur)
I have no output to IDX 247. , it gives me the number "0"
Can someone help me with this?
In the Log I get this information
EventSystem: in Script #1: [string "local function DeviceUpdate(idx, value1)..."]:16: attempt to index global 'otherdevices_247' (a nil value)
Here is the script:
local function DeviceUpdate(idx, value1)
local cmd = string.format("%d|0|%.2f", idx, value1)
--print(cmd)
table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
Debug = "YES" -- Debugging aanzetten ("YES") of uitzetten ("NO")
commandArray = {}
local m = os.date('%M')
if (m % 5 == 0) then -- script loopt iedere 5 minuten
-- Variabelen instellen
local temperature_name = 'Gemiddelde Temp buiten'
local temperature_idx = otherdevices_idx[temperature_name]
local uitlees_name = 'Temperature'
local uitlees_idx = otherdevices_idx[uitlees_name]
--Temperaturen van afgelopen 24 uur opvragen
local sQuery = 'curl "http://192.168.1.37:8080/json.htm?type= ... &range=day"'
local handle=assert(io.popen(sQuery))
local raw = handle:read('*all')
handle:close()
--Ontvangen waarden verwerken
json = assert(loadfile '/home/pi/domoticz/scripts/lua/JSON.lua')()
local jsonData = json:decode(raw)
if Debug=='YES' then
print (jsonData.title)
print (jsonData.status)
print (jsonData.result[1].d)
print (jsonData.result[1].te)
end
--Gemiddelde temperatuur van afgelopen 24 uur bepalen
local sum = 0
local ave = 0
local elements = #jsonData.result
for i = 1, elements do
sum = sum + jsonData.result.te
end
ave = sum / elements
if Debug=='YES' then
print('Gemiddeld: '..tostring(ave))
end
--Gemiddelde temperatuur opslaan in virtuele temperatuur sensor
DeviceUpdate(temperature_idx,ave)
end
return commandArray
I want to measure the average temperature from my outside temperature sensor, this is a sensor that measure, temperature and humidity.
I'm not familiar with the Lua scripts.
But I found an example on the the internet
My IDX from the temperature/humidity sensor is IDX 37 (Buiten Temp)
The dummy temperature has the IDX 247 (Gemiddelde buiten temperatuur)
I have no output to IDX 247. , it gives me the number "0"
Can someone help me with this?
In the Log I get this information
EventSystem: in Script #1: [string "local function DeviceUpdate(idx, value1)..."]:16: attempt to index global 'otherdevices_247' (a nil value)
Here is the script:
local function DeviceUpdate(idx, value1)
local cmd = string.format("%d|0|%.2f", idx, value1)
--print(cmd)
table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
Debug = "YES" -- Debugging aanzetten ("YES") of uitzetten ("NO")
commandArray = {}
local m = os.date('%M')
if (m % 5 == 0) then -- script loopt iedere 5 minuten
-- Variabelen instellen
local temperature_name = 'Gemiddelde Temp buiten'
local temperature_idx = otherdevices_idx[temperature_name]
local uitlees_name = 'Temperature'
local uitlees_idx = otherdevices_idx[uitlees_name]
--Temperaturen van afgelopen 24 uur opvragen
local sQuery = 'curl "http://192.168.1.37:8080/json.htm?type= ... &range=day"'
local handle=assert(io.popen(sQuery))
local raw = handle:read('*all')
handle:close()
--Ontvangen waarden verwerken
json = assert(loadfile '/home/pi/domoticz/scripts/lua/JSON.lua')()
local jsonData = json:decode(raw)
if Debug=='YES' then
print (jsonData.title)
print (jsonData.status)
print (jsonData.result[1].d)
print (jsonData.result[1].te)
end
--Gemiddelde temperatuur van afgelopen 24 uur bepalen
local sum = 0
local ave = 0
local elements = #jsonData.result
for i = 1, elements do
sum = sum + jsonData.result.te
end
ave = sum / elements
if Debug=='YES' then
print('Gemiddeld: '..tostring(ave))
end
--Gemiddelde temperatuur opslaan in virtuele temperatuur sensor
DeviceUpdate(temperature_idx,ave)
end
return commandArray