Page 1 of 1

Problem passing variables to UpdateDevice

Posted: Saturday 09 January 2016 23:14
by Philosifer
Hello folks

I'm working on lua script to extract the current conditions from weewx and show them in Domoticz but I just can't work out how to pass variables to UpdateDevice.

I'm sure I'm just missing a subtlety of lua's type handling.

Here's the full script but the problem lines are at the bottom

Code: Select all

-- Title: script_device_weewx.lua
-- Date: 09-01-2016
-- this script reads the weewxpws xml file and uploads current data to virtual device

commandArray = {}

os.execute('wget http://server1/weewx/XML/weewx_pws.xml -O /home/phil/domoticz/scripts/lua/weewx_pws.xml')

local filename = "/home/phil/domoticz/scripts/lua/weewx_pws.xml"

local line = ''
local temperature = 0
local humidity = 0
local pressure = 0
local tempdevice = ''

for line in io.lines(filename) do
   if string.find(line,"\"temp\"") then
      temperature=line
  end

   if string.find(line,"\"hum\"") then
      humidity=line
   end

   if string.find(line,"\"barometer\"") then
      pressure=line
   end
--print(line)
end

-- Update device id 23, nval 0 sval temp;hum;hum-stat;baro;baro-stat
tempdevice='23|0|'..temperature..';'..humidity..';0;'..pressure..';0'

--force print as string
print(string.format("%s",tempdevice))

-- none of these work
--commandArray['UpdateDevice'] = "23|0|" .. temperature .. ";" .. humidity .. ";0;" .. pressure .. ";0"
--commandArray['UpdateDevice'] = '23|0|'..temperature..';'..humidity..';0;'..pressure..';0'
--commandArray['UpdateDevice'] = tempdevice

-- only this one updates the device
commandArray['UpdateDevice'] = '23|0|6.4;99;0;983.1;0'

return commandArray
As you can see i've tried quite a few different methods. The tempdevice string printing out just fine in the log

2016-01-09 22:13:00.184 LUA: 23|0|6.3;99;0;982.8;0

but however I try to post the information to the device it just keeps setting everything to 0

Any ideas. As far as I can see I'm following the example scripts on the wiki pages in the ways i've tried.

Phil

Re: Problem passing variables to UpdateDevice

Posted: Sunday 10 January 2016 9:12
by Toulon7559
< Because the code no longer 'overlaps' the text below the 'codebox', the earlier comment in this message is no longer applicable >

Re: Problem passing variables to UpdateDevice

Posted: Sunday 10 January 2016 11:03
by Philosifer
Hi Toulon

All the code is there. The extra line you might be seeing is a line from the domoticz log showing that the print command produces the correct output from the tempdevice variable

Or are you saying that there should be something after the return command?

Phil

Re: Problem passing variables to UpdateDevice

Posted: Sunday 10 January 2016 11:27
by jvdz
Strange, the script looks correct assuming you only have one UpdateDevice active in the script.

Jos

Re: Problem passing variables to UpdateDevice

Posted: Sunday 10 January 2016 11:45
by Philosifer
Ah. I figured out the problem. Sleeping on it apparently works :D

Its because of some bad coding by me (of course) and what looks like an oddity in the way lua handles variables.

My input file is some XML and having found the right line (below) with the string search

<realtime><data realtime="temp">6.5<!--outsideTemp--></data></realtime>

I put the whole line above into a number variable.

The print statements then just output the number and the string operations even looked right but I suspect that lua was actually storing the whole line in the variable and then mishandling it when passing the output to updateDevice

When I change this

Code: Select all

if string.find(line,"\"temp\"") then
   temperature=line
end
to this

Code: Select all

if string.find(line,"\"temp\"") then
   temperature=tonumber(string.match(line, "%d+"))
end
everything then works as expected.

So here for anyone who'd like to extract data from weewx is the whole script

Code: Select all

-- Title: script_device_weewx.lua
-- Date: 10-01-2016
-- this script reads the weewxpws xml file and uploads current data to virtual device

commandArray = {}

os.execute('wget http://yourserver/weewx/XML/weewx_pws.xml -O /home/phil/domoticz/scripts/lua/weewx_pws.xml')

local filename = "/home/phil/domoticz/scripts/lua/weewx_pws.xml"

local line = ''
local temperature = 0
local humidity = 0
local pressure = 0
local tempdevice = ''

for line in io.lines(filename) do
   if string.find(line,"\"temp\"") then
      temperature=tonumber(string.match(line, "%d+"))
  end

   if string.find(line,"\"hum\"") then
      humidity=tonumber(string.match(line, "%d+"))
   end

   if string.find(line,"\"barometer\"") then
      pressure=tonumber(string.match(line, "%d+"))
   end
end

-- Update device id 23, nval(0) sval (temp;hum;hum-stat;baro;baro-stat)
tempdevice='23|0|'..temperature..';'..humidity..';0;'..pressure..';0'

--print(tempdevice))

commandArray['UpdateDevice'] = tempdevice

return commandArray
Phil

Re: Problem passing variables to UpdateDevice

Posted: Tuesday 17 January 2017 15:45
by freddeolss
Use this script with some mod. And it almost work. If the temp is -8.4 i got only 8. Not the - or the value after the dot.


Skickat från min GT-I9505 via Tapatalk

Re: Problem passing variables to UpdateDevice

Posted: Sunday 12 February 2017 18:18
by Ignacio
I have used this script with success. Thank you.

The problem of not obtaining minus sign and decimals is solved by changing:

temperature=tonumber(string.match(line, "%-?%d+.?%d*"))

i.e. the format of the string.

Good luck

Re: Problem passing variables to UpdateDevice

Posted: Saturday 15 July 2017 19:51
by sailmich
Hi, I installed weewx with DEB package and on another Pi with the python tool setup. In booth I can't find /weewx/XML/weewx_pws.xml. Do I have to create an account on wunderground first?
Thx
Mike

Re: Problem passing variables to UpdateDevice

Posted: Friday 21 July 2017 13:18
by sailmich
Hi answer to myself, might be helpful for noobs like me.
XML/weewx_pws.xml isn't part of a fresh installation weewx.
You have to visit http://www.google.com/url?q=http%3A%2F% ... F87GgPBiFA
follow the instructions on that side, by doing this you will create a folder XML with content weewx_pws.xml.

Mike