Page 1 of 1
Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 21:02
by Mazzokun
Hi, I'm writing a lua script to update a instant WATT counter but I doesn't work.. I think that there is a mistake in json url
Code: Select all
commandArray = {}
if devicechanged['Luci Cancellino'] == 'On' then cancellin = 50 else cancellin = 0 end
if devicechanged['Facciata'] == 'On' then facciata = 21 else facciata = 0 end
if devicechanged['Cupola Esterno'] == 'On' then dome = 8 else dome = 0 end
if devicechanged['Corridoio'] == 'On' then hall = 10 else hall = 0 end
if devicechanged['Cupola Xmas'] == 'On' then cupXmas = 500 else cupXmas = 0 end
if devicechanged['Cancellino Xmas'] == 'On' then canXmas = 400 else canXmas = 0 end
if devicechanged['Lampada Sala'] == 'On' then sala = 10 else sala = 0 end
if devicechanged['Faro dietro'] == 'On' then farodie = 35 else farodie = 0 end
if devicechanged['Faro davanti'] == 'On' then farodav = 40 else farodav = 0 end
if devicechanged['Lanterna facciata'] == 'On' then lantfac = 16 else lantfac = 0 end
if devicechanged['Lanterna nonna'] == 'On' then lantnon = 25 else lantnon = 0 end
if devicechanged['Accento ingresso'] == 'On' then accing = 8 else accing = 0 end
if devicechanged['Lampione'] == 'On' then lampion = 23 else lampion = 0 end
if devicechanged['Presa Volante'] == 'On' then presvol = 300 else presvol = 0 end
if devicechanged['Luce Garage'] == 'On' then lucgar = 4 else lucgar = 0 end
watt= cancellin+facciata+dome+hall+cupXmas+canXmas+sala+farodie+farodav+lantfac+lantnon+accing+lampion+presvol+lucgar
os.execute("curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue={.watt}' " )
return commandArray
Could you help me?

Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 21:48
by simonrg
Mazzokun wrote:Hi, I'm writing a lua script to update a instant WATT counter but I doesn't work.. I think that there is a mistake in json url
Could you help me?

It looks like your quotes in the os.execute are not correct as your variable watt will not be seen as you have placed the quotes.
Code: Select all
commandArray = {}
if devicechanged['Luci Cancellino'] == 'On' then cancellin = 50 else cancellin = 0 end
if devicechanged['Facciata'] == 'On' then facciata = 21 else facciata = 0 end
if devicechanged['Cupola Esterno'] == 'On' then dome = 8 else dome = 0 end
if devicechanged['Corridoio'] == 'On' then hall = 10 else hall = 0 end
if devicechanged['Cupola Xmas'] == 'On' then cupXmas = 500 else cupXmas = 0 end
if devicechanged['Cancellino Xmas'] == 'On' then canXmas = 400 else canXmas = 0 end
if devicechanged['Lampada Sala'] == 'On' then sala = 10 else sala = 0 end
if devicechanged['Faro dietro'] == 'On' then farodie = 35 else farodie = 0 end
if devicechanged['Faro davanti'] == 'On' then farodav = 40 else farodav = 0 end
if devicechanged['Lanterna facciata'] == 'On' then lantfac = 16 else lantfac = 0 end
if devicechanged['Lanterna nonna'] == 'On' then lantnon = 25 else lantnon = 0 end
if devicechanged['Accento ingresso'] == 'On' then accing = 8 else accing = 0 end
if devicechanged['Lampione'] == 'On' then lampion = 23 else lampion = 0 end
if devicechanged['Presa Volante'] == 'On' then presvol = 300 else presvol = 0 end
if devicechanged['Luce Garage'] == 'On' then lucgar = 4 else lucgar = 0 end
watt= cancellin+facciata+dome+hall+cupXmas+canXmas+sala+farodie+farodav+lantfac+lantnon+accing+lampion+presvol+lucgar
command ="curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue="..watt.." ' "
print(command)
os.execute(command)
return commandArray
Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 21:58
by Westcott
Why use Json?
Can't you just use an UpdateDevice?
Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 22:00
by papoo
Code: Select all
os.execute('curl "http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue='..watt' " ')
?
or
Code: Select all
os.execute('curl "http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue='..tostring(watt)' " ')
?
Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 22:20
by Mazzokun
First of all, thank you all;

soFor
Code: Select all
command ="curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue="..watt.." ' "
print(command)
os.execute(command)
return commandArray
gives me this log and the counter doesn't upload (is red):
Code: Select all
2016-05-20 22:09:50.901 EventSystem: reset all events...
2016-05-20 22:09:51.054 Hardware Monitor: Fetching data (System sensors)
2016-05-20 22:09:51.093 LUA: curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue=0 '
2016-05-20 22:10:00.873 EventSystem: Event triggered: script_time_notte_1
2016-05-20 22:10:01.137 Activating Scene/Group: [Luci Notturne Esterno]
2016-05-20 22:10:01.138 Activating Scene/Group Device: Luce Garage (On)
2016-05-20 22:10:01.148 LUA: curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue=4 '
2016-05-20 22:10:01.139 (KmTronic) Lighting 2 (Luce Garage)
2016-05-20 22:10:01.262 Activating Scene/Group Device: Lampione (On)
2016-05-20 22:10:01.268 LUA: curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue=23 '
2016-05-20 22:10:01.262 (KmTronic) Lighting 2 (Lampione)
2016-05-20 22:10:01.353 Activating Scene/Group Device: Lanterna Ingresso (On)
2016-05-20 22:10:01.358 LUA: curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue=0 '
2016-05-20 22:10:01.353 (KmTronic) Lighting 2 (Lanterna Ingresso)
2016-05-20 22:10:01.444 Activating Scene/Group Device: Lanterna Nonna (On)
2016-05-20 22:10:01.449 LUA: curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue=0 '
2016-05-20 22:10:01.444 (KmTronic) Lighting 2 (Lanterna Nonna)
2016-05-20 22:10:01.534 Activating Scene/Group Device: Faro Garage (On)
2016-05-20 22:10:01.540 LUA: curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue=0 '
2016-05-20 22:10:01.535 (KmTronic) Lighting 2 (Faro Garage)
2016-05-20 22:10:01.624 Activating Scene/Group Device: Faro Retro (On)
2016-05-20 22:10:01.630 LUA: curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue=0 '
2016-05-20 22:10:01.625 (KmTronic) Lighting 2 (Faro Retro)
2016-05-20 22:10:01.716 Activating Scene/Group Device: Accento Ingresso (On)
2016-05-20 22:10:01.723 LUA: curl 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue=0 '
2016-05-20 22:10:01.717 (KmTronic) Lighting 2 (Accento Ingresso)
For
Code: Select all
os.execute('curl "http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=45&nvalue=0&svalue='..tostring(watt)' " ')
error in log
Code: Select all
Error: EventSystem: in script_device_watt: [string "commandArray = {}..."]:20: attempt to call global 'curl' (a nil value)
@Westcott I tried before, but I'not able to use UpdateDevice; could you give me the right comand please?
Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 22:27
by papoo
Code: Select all
commandArray[1] = {['UpdateDevice'] = "45|0|" .. watt}
Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 22:34
by Mazzokun
Code: Select all
commandArray[1] = {['UpdateDevice'] = "45|0|" .. watt}
now the counter isn't red but it still has 0 value

obvious I turned on some switch
where's the mistake?

Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 22:43
by papoo
Update device/sensor values using commandArray['UpdateDevice']='idx|nValue|sValue'
try
to know the watt value
and try
Code: Select all
commandArray[1] = {['UpdateDevice'] = "45|0|100"}
to set at 100 manually the sValue
Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 23:15
by Mazzokun
Seems that it doesn't like watt= a+b+c+d+e... etc
Code: Select all
2016-05-20 23:10:24.242 EventSystem: reset all events...
2016-05-20 23:10:52.739 Hardware Monitor: Fetching data (System sensors)
2016-05-20 23:10:52.775 LUA: 0
2016-05-20 23:10:52.776 EventSystem: Script event triggered: script_device_watt
2016-05-20 23:10:59.379 User: Davide initiated a switch command
2016-05-20 23:10:59.394 LUA: 10
2016-05-20 23:10:59.395 EventSystem: Script event triggered: script_device_watt
2016-05-20 23:10:59.380 (KmTronic) Lighting 2 (Corridoio)
2016-05-20 23:11:00.032 EventSystem: Event triggered: script_time_notte_1
2016-05-20 23:11:00.077 Activating Scene/Group: [Luci Notturne Esterno]
2016-05-20 23:11:00.078 Activating Scene/Group Device: Luce Garage (On)
2016-05-20 23:11:00.088 LUA: 4
2016-05-20 23:11:00.089 EventSystem: Script event triggered: script_device_watt
2016-05-20 23:11:00.078 (KmTronic) Lighting 2 (Luce Garage)
2016-05-20 23:11:00.142 Activating Scene/Group Device: Lampione (On)
2016-05-20 23:11:00.165 LUA: 23
2016-05-20 23:11:00.166 EventSystem: Script event triggered: script_device_watt
2016-05-20 23:11:00.153 (KmTronic) Lighting 2 (Lampione)
2016-05-20 23:11:00.218 Activating Scene/Group Device: Lanterna Ingresso (On)
2016-05-20 23:11:00.239 LUA: 0
2016-05-20 23:11:00.240 EventSystem: Script event triggered: script_device_watt
2016-05-20 23:11:00.229 (KmTronic) Lighting 2 (Lanterna Ingresso)
2016-05-20 23:11:00.292 Activating Scene/Group Device: Lanterna Nonna (On)
2016-05-20 23:11:00.314 LUA: 0
2016-05-20 23:11:00.315 EventSystem: Script event triggered: script_device_watt
2016-05-20 23:11:00.304 (KmTronic) Lighting 2 (Lanterna Nonna)
2016-05-20 23:11:00.367 Activating Scene/Group Device: Faro Garage (On)
2016-05-20 23:11:00.392 LUA: 0
2016-05-20 23:11:00.393 EventSystem: Script event triggered: script_device_watt
2016-05-20 23:11:00.379 (KmTronic) Lighting 2 (Faro Garage)
2016-05-20 23:11:00.445 Activating Scene/Group Device: Faro Retro (On)
2016-05-20 23:11:00.465 LUA: 0
2016-05-20 23:11:00.466 EventSystem: Script event triggered: script_device_watt
2016-05-20 23:11:00.454 (KmTronic) Lighting 2 (Faro Retro)
2016-05-20 23:11:00.518 Activating Scene/Group Device: Accento Ingresso (On)
2016-05-20 23:11:00.542 LUA: 0
2016-05-20 23:11:00.543 EventSystem: Script event triggered: script_device_watt
2016-05-20 23:11:00.530 (KmTronic) Lighting 2 (Accento Ingresso)
2016-05-20 23:11:22.772 Hardware Monitor: Fetching data (System sensors)
2016-05-20 23:11:22.808 LUA: 0
2016-05-20 23:11:22.809 EventSystem: Script event triggered: script_device_watt
2016-05-20 23:11:50.845 Error: Webserver: File '/styles/elemental/images/bg-track.png': No such file or directory (2)
2016-05-20 23:11:52.805 Hardware Monitor: Fetching data (System sensors)
2016-05-20 23:11:52.840 LUA: 0
2016-05-20 23:11:52.841 EventSystem: Script event triggered: script_device_watt
Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 23:27
by papoo
and when you set at 100 manually the sValue ?
Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 23:42
by Mazzokun
yes, if I put
commandArray[1] = {['UpdateDevice'] = "45|0|100"}
works

Re: Lua Script tu update a device with json url -help
Posted: Friday 20 May 2016 23:54
by papoo
Code: Select all
commandArray[1] = {['UpdateDevice'] = "45|0|" .. tostring(watt)}
?
Re: Lua Script tu update a device with json url -help
Posted: Saturday 21 May 2016 0:37
by Mazzokun
there are no errors in the log, 0 to the counter.. I think the script does not properly perform the sum of watts

Re: Lua Script tu update a device with json url -help
Posted: Saturday 21 May 2016 1:00
by simonrg
When the script is called only one device will have changed so you can simplify your script by using elseif and just set watt directly.
Code: Select all
commandArray = {}
watt = 0
if devicechanged['Luci Cancellino'] == 'On' then watt = 50
elseif devicechanged['Facciata'] == 'On' then watt = 21
elseif devicechanged['Cupola Esterno'] == 'On' then watt = 8
elseif devicechanged['Corridoio'] == 'On' then watt = 10
elseif devicechanged['Cupola Xmas'] == 'On' then watt = 500
elseif devicechanged['Cancellino Xmas'] == 'On' then watt = 400
elseif devicechanged['Lampada Sala'] == 'On' then watt = 10
elseif devicechanged['Faro dietro'] == 'On' then watt = 35
elseif devicechanged['Faro davanti'] == 'On' then watt = 40
elseif devicechanged['Lanterna facciata'] == 'On' then watt = 16
elseif devicechanged['Lanterna nonna'] == 'On' then watt = 25
elseif devicechanged['Accento ingresso'] == 'On' then watt = 8
elseif devicechanged['Lampione'] == 'On' then watt = 23
elseif devicechanged['Presa Volante'] == 'On' then watt = 300
elseif devicechanged['Luce Garage'] == 'On' then watt = 4
end
commandArray['UpdateDevice'] = "45|0|" .. tostring(watt)
return commandArray
Re: Lua Script tu update a device with json url -help
Posted: Saturday 21 May 2016 1:03
by simonrg
Thinking about suspect this is not what you want, you are looking to sum the energy of each device to get total energy being used.
To make a sum you are going to need to store the sum as a user variable, then add to this each time a device is switched on and subtract from it each time a device is switched off.
Re: Lua Script tu update a device with json url -help
Posted: Saturday 21 May 2016 8:44
by Mazzokun
@simonrg Exactly!
you are looking to sum the energy of each device to get total energy being used
How can I do this?
To make a sum you are going to need to store the sum as a user variable, then add to this each time a device is switched on and subtract from it each time a device is switched off.
Thank's
Re: Lua Script tu update a device with json url -help
Posted: Saturday 21 May 2016 12:07
by simonrg
Mazzokun wrote:@simonrg Exactly!
you are looking to sum the energy of each device to get total energy being used
How can I do this?
To make a sum you are going to need to store the sum as a user variable, then add to this each time a device is switched on and subtract from it each time a device is switched off.
Thank's
The other way to do it is to test the devices each time any device changes or once a minute, which may be simpler.
Probably best to have this as time script rather than a device script, then it will only update every minute, rather every time anything changes.
Code: Select all
-- script_device_power_frequent.lua - a device type for the lua script will do the sum every time any device changes
-- script_time_power_every_minute.lua - a time type for the lua script will do the sum only every minute
commandArray = {}
watt = 0
if otherdevices['Luci Cancellino'] == 'On' then watt = watt + 50 end
if otherdevices['Facciata'] == 'On' then watt = watt + 21 end
if otherdevices['Cupola Esterno'] == 'On' then watt = watt + 8 end
if otherdevices['Corridoio'] == 'On' then watt = watt + 10 end
if otherdevices['Cupola Xmas'] == 'On' then watt = watt + 500 end
if otherdevices['Cancellino Xmas'] == 'On' then watt = watt + 400 end
if otherdevices['Lampada Sala'] == 'On' then watt = watt + 10 end
if otherdevices['Faro dietro'] == 'On' then watt = watt + 35 end
if otherdevices['Faro davanti'] == 'On' then watt = watt + 40 end
if otherdevices['Lanterna facciata'] == 'On' then watt = watt + 16 end
if otherdevices['Lanterna nonna'] == 'On' then watt = watt + 25 end
if otherdevices['Accento ingresso'] == 'On' then watt = watt + 8 end
if otherdevices['Lampione'] == 'On' then watt = watt + 23 end
if otherdevices['Presa Volante'] == 'On' then watt = watt + 300 end
if otherdevices['Luce Garage'] == 'On' then watt = watt + 4 end
commandArray['UpdateDevice'] = "45|0|" .. tostring(watt)
return commandArray
EDIT - corrected watt = watt to watt = 0
Re: Lua Script tu update a device with json url -help
Posted: Saturday 21 May 2016 13:30
by Mazzokun
Doesn't work

Log:
Code: Select all
2016-05-21 13:28:15.686 User: Davide initiated a switch command
2016-05-21 13:28:15.694 Error: EventSystem: in script_device_powerfrequent: [string "-- script_device_power_frequent.lua - a devic..."]:10: attempt to perform arithmetic on global 'watt' (a nil value)
2016-05-21 13:28:15.686 (KmTronic) Lighting 2 (Corridoio)
2016-05-21 13:28:22.523 Error: EventSystem: in script_device_powerfrequent: [string "-- script_device_power_frequent.lua - a devic..."]:10: attempt to perform arithmetic on global 'watt' (a nil value)
Re: Lua Script tu update a device with json url -help
Posted: Saturday 21 May 2016 13:53
by Mazzokun
Re: Lua Script tu update a device with json url -help
Posted: Sunday 22 May 2016 8:36
by simonrg
Glad it all works now.
Mazzokun wrote:Doesn't work

Log:
Code: Select all
2016-05-21 13:28:15.686 User: Davide initiated a switch command
2016-05-21 13:28:15.694 Error: EventSystem: in script_device_powerfrequent: [string "-- script_device_power_frequent.lua - a devic..."]:10: attempt to perform arithmetic on global 'watt' (a nil value)
2016-05-21 13:28:15.686 (KmTronic) Lighting 2 (Corridoio)
2016-05-21 13:28:22.523 Error: EventSystem: in script_device_powerfrequent: [string "-- script_device_power_frequent.lua - a devic..."]:10: attempt to perform arithmetic on global 'watt' (a nil value)
The error messages are quite helpful to pick up small errors.
What it is telling you is that on line 10 of script_device_power_frequent.lua there is a variable called watt which hasn't got a value.
Line 10 is watt = watt, if you look further up the script then you can see that watt hasn't been set before, hence watt has no value at this point, also watt = watt doesn't look right.
So line 10 should be watt = 0, this sets watt prior to doing the summing further on, hence watt is not used before its value is set.