I’m importing temperature and humidity from a Tellstick Net sensor and store them in a file on my Raspberry Pi, then I want a LUA-script to place these values in to a virtual sensor, but I cannot figure out how to make this happen. I’m not so familiar with LUA-programming, but I have modifyed another LUA-script that I use for a wheather station where I only "scraping" the temperature as an referens.
I have installed a virtual sensor for temperature and humidity (Idx=45) and the file with temperature and humidity data contains the values “21;45” (+21 degrees Celsius and 45 % humidity).
Then I use a LUA-script that reads the file with the semicolon separated temperature and humidity data and place these values into variables that is sent to the virtual sensor “idxt” and “idxh”. I use another “real” temperature sensor (“Utetemp”) as an trigger for the script :
Code: Select all
--
--The following need updated for your environment get the 'Idx' or 'Name' off the Device tab. By default only the Temp is 'uncommented or enabled' in this script.
local sensorwu = 'Utetemp' --name of the sensor that gets created when you add the WU device (and that contains multiple values like temperature, humidity, barometer etc)
local idxt = 45 --idx of the virtual temperature sensor you need to change this to your own Device IDx
local idxh = 45 --idx of the virtual humidity sensor you need to change this to your own Device IDx
--
commandArray = {}
if devicechanged[sensorwu] then
f = assert (io.open ("/home/pi/tellstick/data/temp-temp+hum.txt", "r")) -- open the file with the temperature and humidity informatiom
sTempHum = f:read ("*a") -- read all data in file
print (sTempHum) -- print out temperature and humidity (separated by a semicolon)
f:close () -- close file
sWeatherTemp, sWeatherHumidity = sTempHum:match("([^;]+);([^;]+)")
sWeatherTemp = tonumber(sWeatherTemp)
sWeatherHumidity = tonumber(sWeatherHumidity)
print(sWeatherTemp) -- print temperature
print(sWeatherHumidity) -- print humidity
commandArray[1] = {['UpdateDevice'] = idxt .. '|0|' .. sWeatherTemp}
commandArray[2] = {['UpdateDevice'] = idxh .. '|' .. tostring(sWeatherHumidity)}
end
return commandArray
The virtual sensor is showing nothing:
The actual virtual sensor values after running LUA-script (from “show current states”):
What could be wrong in LUA-script?