Assistance required getting energy script to work, split string

Must be cleaned all topics go to the right subforum
Post Reply
chrisfraser05
Posts: 33
Joined: Sunday 08 January 2017 19:59
Target OS: Windows
Domoticz version:
Contact:

Assistance required getting energy script to work, split string

Post by chrisfraser05 » Tuesday 10 January 2017 12:54

Hi guys.

Hoping someone might be able to help me with my script. I've not really done much with LUA before but really need to start learning.
I've been pouring over the forum and I've started getting it working but I'm not quite there.


First the purpose of the script.

I want to read my owl energy meter each time it updates, then transfer the Power and Energy values to a dummy device on Vera.

I've created 3 user variables, "energy1", "PowerF" and "EnergyF"

The script updates the string "energy1" no problem. I have no errors in the log.

I am unsure how to split this string into the two seperate values for Power and KWH.
What I have done below simply does nothing.
I've tried quite a few different things but I'm just not getting it.

I've added in the url's with which I eventually want to send the values over to Vera, but at the moment my main focus is being able to see them in the user variables so I can understand what is going on.

Lastly, the values I read for example are "482.000;764578.875" the split is ;
so 482.000 is the power and 764578.875 is Energy. I would like to divide and round the energy value down to get it to 765 in this example.


Thanks in advance guys. :roll:

Code: Select all

commandArray = {}
energyreading = "Energy Monitor"

if (devicechanged[energyreading]) then
   print( " ####watt debug: "..devicechanged[energyreading])
   commandArray['Variable:energy1']=tostring(devicechanged[energyreading])
  
  watt, kwh = energyreading:match("([^;]+);([^;]+)")
  
  commandArray['Variable:PowerF']=watt
  commandArray['Variable:EnergyF']=kwh
  
  
--http://192.168.1.12:3480/data_request?id=variableset&DeviceNum=271&serviceId=urn:micasaverde-com:serviceId:EnergyMetering1&Variable=WATT&Value=..watt
  
--http://192.168.1.12:3480/data_request?id=variableset&DeviceNum=271&serviceId=urn:micasaverde-com:serviceId:EnergyMetering1&Variable=KWH&Value=..kwh
  
end

return commandArray

elmortero
Posts: 129
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: Assistance required getting energy script to work, split string

Post by elmortero » Tuesday 10 January 2017 13:09

I think this wiki page will show you how to do that (splitting the string):
https://www.domoticz.com/wiki/Capturing ... ua_Scripts

And for the rounding part :
viewtopic.php?t=7642

chrisfraser05
Posts: 33
Joined: Sunday 08 January 2017 19:59
Target OS: Windows
Domoticz version:
Contact:

Re: Assistance required getting energy script to work, split string

Post by chrisfraser05 » Tuesday 10 January 2017 17:12

Hi Elmortero.

Thanks for your reply!

I was using that first link last night to try and figure something out but it just wasn't sinking in.

I tried writing all sorts of variations on that and couldn't get anything to work.

elmortero
Posts: 129
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: Assistance required getting energy script to work, split string

Post by elmortero » Tuesday 10 January 2017 17:49

try

Code: Select all

watt, kwh = string.match(otherdevices_svalues['Energy Monitor'], "(%d+%.*%d*);(%d+%.*%d*)")
instead of

Code: Select all

watt, kwh = energyreading:match("([^;]+);([^;]+)")

elmortero
Posts: 129
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: Assistance required getting energy script to work, split string

Post by elmortero » Tuesday 10 January 2017 18:31

I have used that script long time ago and remember struggling quite a lot with it :-)
In the end it worked, but don't really remember quite how.
Short after that I moved to dzvents which has a lot of built-in functions for these kind of things.
your script could be something like:

Code: Select all

return {
    active = true,
    on = {
        'Energy Monitor'
    },
    execute = function(domoticz)
		local e-watts = domoticz.devices['Energy Monitor'].WActual
		local e-kwh= domoticz.devices['Energy Monitor'].WhTotal    	
domoticz.openUrl("http://192.168.1.12:3480/data_request?id=variableset&DeviceNum=271&serviceId=urn:micasaverde-com:serviceId:EnergyMetering1&Variable=KWH&Value=" ..e-kwh)
domoticz.openUrl("http://192.168.1.12:3480/data_request?id=variableset&DeviceNum=271&serviceId=urn:micasaverde-com:serviceId:EnergyMetering1&Variable=WATT&Value=" ..e-watts)
    end
}
Have a look at it, it could be worth it.

chrisfraser05
Posts: 33
Joined: Sunday 08 January 2017 19:59
Target OS: Windows
Domoticz version:
Contact:

Re: Assistance required getting energy script to work, split string

Post by chrisfraser05 » Tuesday 10 January 2017 19:35

Thanks again for all your help.

We're cooking on gas.

I have the seperated values printing in the log now!

Code: Select all

commandArray = {}
energyreading = "Energy Monitor"


if (devicechanged[energyreading]) then

print( " ####combined debug: "..devicechanged[energyreading])

watt, kwh = string.match(otherdevices_svalues['Energy Monitor'], "(%d+%.*%d*);(%d+%.*%d*)")

print( " ####splitwatts debug: "..watt)
print( " ####splitkwh debug: "..kwh)

end

return commandArray
So next I want to send the values (lets ignore the math for now).

I'm going to try and get that working.

chrisfraser05
Posts: 33
Joined: Sunday 08 January 2017 19:59
Target OS: Windows
Domoticz version:
Contact:

Re: Assistance required getting energy script to work, split string

Post by chrisfraser05 » Tuesday 10 January 2017 21:00

I'm getting somewhere now but....


I can have one URL call and it works. If I do two only the second works.

No errors in log!
commandArray = {}
energyreading = "Energy Monitor"


if (devicechanged[energyreading]) then

print( " ####combined debug: "..devicechanged[energyreading])

watt, kwh = string.match(otherdevices_svalues['Energy Monitor'], "(%d+%.*%d*);(%d+%.*%d*)")

print( " ####splitwatts debug: "..watt)
print( " ####splitkwh debug: "..kwh)

commandArray['OpenURL']="http://192.168.1.12:3480/data_request?i ... tts&Value=" ..watt

-- commandArray['OpenURL']="http://192.168.1.12:3480/data_request?i ... KWH&Value=" ..kwh

end

return commandArray

User avatar
Egregius
Posts: 2416
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Assistance required getting energy script to work, split string

Post by Egregius » Tuesday 10 January 2017 21:25

Use 2 times os.execute with curl.

chrisfraser05
Posts: 33
Joined: Sunday 08 January 2017 19:59
Target OS: Windows
Domoticz version:
Contact:

Re: Assistance required getting energy script to work, split string

Post by chrisfraser05 » Tuesday 10 January 2017 22:17

Egregius.

Thanks for your post. I just had a quick read up on curl, funnily enough I found another thread where you are involved with curl.

I take it that involves installing curl onto the PC and putting it in the correct folder for it to be called in the script?

I'll do that later to make things nice and neat.

For the mean time I've just made two scripts, one for energy and one for power.

Imperihome rounds the values for display and on the graphs so I've divided the KWH reading but not bothered with rounding.


Thanks for both of your help. I'm learning now slowly!

chrisfraser05
Posts: 33
Joined: Sunday 08 January 2017 19:59
Target OS: Windows
Domoticz version:
Contact:

Re: Assistance required getting energy script to work, split string

Post by chrisfraser05 » Wednesday 11 January 2017 0:41

just for the fun of it I've offset the reading to match my Energy supplier meter to save me having to go to the cabinet to update my online billing

Code: Select all

commandArray = {}

energyreading = "Energy Monitor"

if (devicechanged[energyreading]) then

-- print( " ####combined debug: "..devicechanged[energyreading])

watt, kwhraw = string.match(otherdevices_svalues['Energy Monitor'], "(%d+%.*%d*);(%d+%.*%d*)")

kwhdiv = (kwhraw / 1000 )
kwh = (kwhdiv + 12553)
-- print( " ####splitkwh debug: "..kwh)

 commandArray['OpenURL']="http://192.168.1.12:3480/data_request?id=variableset&DeviceNum=271&serviceId=urn:micasaverde-com:serviceId:EnergyMetering1&Variable=KWH&Value=" ..kwh

end

return commandArray

Nautilus
Posts: 653
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: Assistance required getting energy script to work, split string

Post by Nautilus » Wednesday 11 January 2017 9:12

chrisfraser05 wrote:I'm getting somewhere now but....


I can have one URL call and it works. If I do two only the second works.

No errors in log!
commandArray = {}
energyreading = "Energy Monitor"


if (devicechanged[energyreading]) then

print( " ####combined debug: "..devicechanged[energyreading])

watt, kwh = string.match(otherdevices_svalues['Energy Monitor'], "(%d+%.*%d*);(%d+%.*%d*)")

print( " ####splitwatts debug: "..watt)
print( " ####splitkwh debug: "..kwh)

commandArray['OpenURL']="http://192.168.1.12:3480/data_request?i ... tts&Value=" ..watt

-- commandArray['OpenURL']="http://192.168.1.12:3480/data_request?i ... KWH&Value=" ..kwh

end

return commandArray
Do it like this and it should work without curl as well:

Code: Select all

commandArray[#commandArray + 1] = {['OpenURL']="http://192.168.1.12:3480/data_request?id=variableset&DeviceNum=271&serviceId=urn:micasaverde-com:serviceId:EnergyMetering1&Variable=Watts&Value=" ..watt}
commandArray[#commandArray + 1] = {['OpenURL']="http://192.168.1.12:3480/data_request?id=variableset&DeviceNum=271&serviceId=urn:micasaverde-com:serviceId:EnergyMetering1&Variable=KWH&Value=" ..kwh}

chrisfraser05
Posts: 33
Joined: Sunday 08 January 2017 19:59
Target OS: Windows
Domoticz version:
Contact:

Re: Assistance required getting energy script to work, split string

Post by chrisfraser05 » Thursday 12 January 2017 23:09

I'll give that one a go later. Thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests