Page 1 of 1

script help

Posted: Thu Jan 05, 2017 11:54 am
by Edwin
h i need some help withthis lua script, "i get the error xml word niet goed gelezen" what is my mistake?
i added the xml file and the script file
please help
thanks.

xml file:

Code: Select all

<appliances>
<appliance>
<macaddress>000D6F00003FF4FA</macaddress>
<name>CloseInBoiler</name>
<currentpowerusagewatts>0</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FEC94</macaddress>
<name>CV-ketel</name>
<currentpowerusagewatts>9</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FF1DD</macaddress>
<name>Droger</name>
<currentpowerusagewatts>3</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F0000384709</macaddress>
<name>HsProServer</name>
<currentpowerusagewatts>0</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FEE9C</macaddress>
<name>Koelkast</name>
<currentpowerusagewatts>92</currentpowerusagewatts>
</appliance>
lua script:

Code: Select all


commandArray = {}



function XML_Capture(cmd,flatten)
   local f = assert(io.popen(cmd, 'r'))
   local s = assert(f:read('*a'))
   f:close()
   if flatten  then
      s = string.gsub(s, '^%s+', '')
      s = string.gsub(s, '%s+$', '')
      s = string.gsub(s, '[\n\r]+', ' ')
   end
   return s
end

--zet up TRUE op in logfile weer te geven
debug = true

--vul hier de idx in van domoticz en hun namen
CloseInBoiler = 219
--CVketel = 216
--Droger = 217
--HsProServer = 218
--HWTemp_Flow = 108
--HWTemp_Return = 109
--HWTemp_Tank_Top = 110
--HWTemp_Tank_Bottom = 111

--vul hier de id`s of mac adressen in op volgorde
--kunnen nog meer bij gezet worden

sensorID_1 = "000D6F00003FF4FA"
--sensorID_2 = "000D6F00003FEC94"
--sensorID_3 = "000D6F00003FF1DD"
--sensorID_4 = "000D6F0000384709"


-- plaats hier het ipadres van plugwise

Device_IP = "192.168.2.167:xx"

if debug == true then
        print("Reading values from: 'http://"..Device_IP.."/xml/edwin.xml'")
end

-- de xml file van plugwise

XML_string=XML_Capture("curl -s 'http://"..Device_IP.."/xml/edwin.xml'",1)


valid = string.find(XML_string, "<response>")    -- controle

    if debug == true then
        print(XML_string)
    end

    if valid == nil then
        print ("Xml word niet goed gelezen,Geen Update")
    else

--vind de eerste sensor op zijn unieke id

    i = string.find(XML_string,sensorID_1)          -- vind de eerste sensor op uid
    p = string.find(XML_string,"<currentpowerusagewatts>",i)          --zoek voor de eerste verschijning van <currentpowerusagewatts> na uid
   CloseInBoiler=string.sub(XML_string,p+0,p+0)  --het verbruik is 5 karakters na  <currentpowerusagewatts> - i.e. 24-26

    commandArray[1] = {['UpdateDevice'] = CloseInBoiler.."|0|"..CloseInBoiler} --stuur geupdate waardes naar Domoticz

        if debug == true then
            print("temp returned = ".."'"..CloseInBoiler.."'")
        end






    end




return commandArray

Re: script help

Posted: Thu Jan 05, 2017 7:01 pm
by yjb
Without looking further it seems that you are missing the closing quote of the appliances branch:

Code: Select all

</appliances>

Re: script help

Posted: Thu Jan 05, 2017 9:12 pm
by Edwin
Thanks for your help,
But where you mean?

Re: script help

Posted: Thu Jan 05, 2017 9:52 pm
by yjb
Hi,

Since you start the entire definition with the following tag (appliances):

Code: Select all

<appliances>
Everything that follows is part of that definition, but at some point XML expects an "ending" tag as well. As definitions are balanced, the 1st tag is also the closing tag of the entire definition

Code: Select all

</appliances>
There might be other stuff wrong, but from an XML perspective you are missing a closing delimiter.

Re: script help

Posted: Thu Jan 05, 2017 11:44 pm
by Edwin
Sorry this is the xml file:
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<appliances>
<%
if ((Request.get["mac"] != undefined) && (Request.get["mac"] != ''))
$macfilter = Request.get["mac"]
else
$macfilter = ''
/if

foreach Plugwise.Appliances
$appliance = $_Value
if (($macfilter == '') || ($macfilter == $appliance.Module.MacAddress))
echo "<appliance>"
echo "<macaddress>",$appliance.Module.MacAddress,"</macaddress>"
echo "<name>",$appliance.Name.Replace("'","`").Replace("&","+").Replace("<","("),"</name>"
echo "<currentpowerusagewatts>",$appliance.PowerUsage,"</currentpowerusagewatts>"
echo "</appliance>\n"




/if
/foreach
%>
</appliances>


The one in my first post is the outcome in internet explorer.
I think its something else or am i wrong?

Re: script help

Posted: Fri Jan 06, 2017 9:11 am
by yjb
I'm sorry, I can't help you with the lua stuff, as I do not really have experience. I would; check again in a flatfile editor that the trailing /appliances is really part of the xml (as it should be).

Re: script help

Posted: Fri Jan 06, 2017 10:02 am
by Edwin
Ok
Thanks for youre help