Problem passing variables to UpdateDevice

Moderator: leecollings

Post Reply
Philosifer
Posts: 17
Joined: Thursday 07 January 2016 18:38
Target OS: Linux
Domoticz version: 2.4223
Location: United Kingdom
Contact:

Problem passing variables to UpdateDevice

Post 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
Toulon7559
Posts: 849
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: Problem passing variables to UpdateDevice

Post by Toulon7559 »

< Because the code no longer 'overlaps' the text below the 'codebox', the earlier comment in this message is no longer applicable >
Last edited by Toulon7559 on Sunday 10 January 2016 14:41, edited 4 times in total.
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
Philosifer
Posts: 17
Joined: Thursday 07 January 2016 18:38
Target OS: Linux
Domoticz version: 2.4223
Location: United Kingdom
Contact:

Re: Problem passing variables to UpdateDevice

Post 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
User avatar
jvdz
Posts: 2269
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Problem passing variables to UpdateDevice

Post by jvdz »

Strange, the script looks correct assuming you only have one UpdateDevice active in the script.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Philosifer
Posts: 17
Joined: Thursday 07 January 2016 18:38
Target OS: Linux
Domoticz version: 2.4223
Location: United Kingdom
Contact:

Re: Problem passing variables to UpdateDevice

Post 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
freddeolss
Posts: 2
Joined: Tuesday 17 January 2017 15:38
Target OS: -
Domoticz version:
Contact:

Re: Problem passing variables to UpdateDevice

Post 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
Ignacio
Posts: 4
Joined: Sunday 12 February 2017 18:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Problem passing variables to UpdateDevice

Post 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
sailmich
Posts: 245
Joined: Wednesday 17 February 2016 22:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Germany
Contact:

Re: Problem passing variables to UpdateDevice

Post 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
sailmich
Posts: 245
Joined: Wednesday 17 February 2016 22:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Germany
Contact:

Re: Problem passing variables to UpdateDevice

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests