script help

Moderator: leecollings

Post Reply
edwin1234
Posts: 287
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

script help

Post by edwin1234 »

i try to use a script original from rata1,
but cant get it together, i want to read the PowerUsage of the CloseInBoiler out of an xml and send data to domoticz.
i`m doing something wrong but cant figure it out.
heres the xml:

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>0</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FF596</macaddress>
<name>Magnetron</name>
<currentpowerusagewatts>1</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FEEA4</macaddress>
<name>MediaCenter</name>
<currentpowerusagewatts>0</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FE4C3</macaddress>
<name>Multimedia</name>
<currentpowerusagewatts>32</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FEE89</macaddress>
<name>ongebruikt</name>
<currentpowerusagewatts>0</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FE733</macaddress>
<name>Ongebruikt2</name>
<currentpowerusagewatts>0</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FEDFC</macaddress>
<name>PcEd</name>
<currentpowerusagewatts>0</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FE4E9</macaddress>
<name>Router</name>
<currentpowerusagewatts>27</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FF6D1</macaddress>
<name>Senseo</name>
<currentpowerusagewatts>0</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FEF52</macaddress>
<name>Televisie</name>
<currentpowerusagewatts>87</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FE4E0</macaddress>
<name>TvSlaapkamer</name>
<currentpowerusagewatts>0</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FD7C2</macaddress>
<name>VijverPomp</name>
<currentpowerusagewatts>2</currentpowerusagewatts>
</appliance>
<appliance>
<macaddress>000D6F00003FD2F9</macaddress>
<name>Wasmachine</name>
<currentpowerusagewatts>0</currentpowerusagewatts>
</appliance>
</appliances>
and this is the script:

Code: Select all

------------------------------------------------------------------------------------------
-- Version 2b 20160430
--
-- Domoticz lua script to convert XML output from FP4All_PVLogger
-- Reads the status based on the unique IP of the PVLogger and passes values
-- to virtual sensor(s) in Domoticz
-- Complete for Power & Energy, prepared for Temperature, Voltage and Current
--------------------------------------------------------------------------------------------------
-- Original script by RATA1 at Domoticz-forum, adapted by Toulon7559 (c)2016
--------------------------------------------------------------------------------------------------

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

-- Line 26: Set debug to true to print to log file.
debug = true

-- Line 29: Define the idx of your virtual sensors - i.e. what you have called your sensors and their IDX numbers
CloseInBoiler = 211

-- Line 37: Define your device IP-address

Device_IP = "192.168.2.197:7778"

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

-- Line 45: Read the XML data from the device

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

valid = string.find(XML_string, "<response>")    -- check we are looking in the right place

    if debug == true then
        print(XML_string)
    end

    if valid == nil then
        print ("Bad XML status read - info NOT updated")
    else

-- Line 59: Find in the XML_string the info-fields based on their labels
-- Determine start/stop-positions of the values within the substrings

    p = string.find(XML_string,"PowerUsage=")    -- read position of power string


-- Line 82: Extract the values and process for upload to Domoticz


-- Line 115


-- Line 127: Upload to Domoticz; to be expanded for upload of other values to related IDXes

    commandArray[1] = {['UpdateDevice'] = CloseInBoiler.."|0|"}  -- send updated values to Domoticz

        if debug == true then
            print("CloseInBoiler = )
        end

    end

return commandArray
Last edited by edwin1234 on Saturday 14 January 2017 12:03, edited 1 time in total.
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: script help

Post by Egregius »

edwin1234
Posts: 287
Joined: Sunday 09 October 2016 20:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Nederland
Contact:

Re: script help

Post by edwin1234 »

Yes. i have got it working.
but i now get an output like this:
CloseInBoiler = '0<'
CVketel = '8<'
Droger = '0<'
HsProServer = '0<'
How do i get rid of the "<" symbol and still can catch a value with 2 numbers?
like CVketel =18
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest