Lua Script tu update a device with json url -help

Moderator: leecollings

Post Reply
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Lua Script tu update a device with json url -help

Post 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 :cry:

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&param=udevice&idx=45&nvalue=0&svalue={.watt}' " )
return commandArray
Could you help me? :P
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script tu update a device with json url -help

Post 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 :cry:
Could you help me? :P
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&param=udevice&idx=45&nvalue=0&svalue="..watt.." ' "
print(command)
os.execute(command)

return commandArray
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: Lua Script tu update a device with json url -help

Post by Westcott »

Why use Json?
Can't you just use an UpdateDevice?
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: Lua Script tu update a device with json url -help

Post by papoo »

Code: Select all

os.execute('curl "http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=45&nvalue=0&svalue='..watt' " ')
?
or

Code: Select all

os.execute('curl "http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=45&nvalue=0&svalue='..tostring(watt)' " ')
?
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: Lua Script tu update a device with json url -help

Post by Mazzokun »

First of all, thank you all; ;)
soFor

Code: Select all

command ="curl 'http://127.0.0.1:8080/json.htm?type=command&param=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&param=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?
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: Lua Script tu update a device with json url -help

Post by papoo »

Code: Select all

commandArray[1] = {['UpdateDevice'] = "45|0|" .. watt}
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: Lua Script tu update a device with json url -help

Post by Mazzokun »

Code: Select all

commandArray[1] = {['UpdateDevice'] = "45|0|" .. watt}
now the counter isn't red but it still has 0 value :shock: obvious I turned on some switch :lol:
where's the mistake? :cry:
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: Lua Script tu update a device with json url -help

Post by papoo »

Update device/sensor values using commandArray['UpdateDevice']='idx|nValue|sValue'

try

Code: Select all

print(watt)
to know the watt value
and try

Code: Select all

commandArray[1] = {['UpdateDevice'] = "45|0|100"}
to set at 100 manually the sValue
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: Lua Script tu update a device with json url -help

Post 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
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: Lua Script tu update a device with json url -help

Post by papoo »

and when you set at 100 manually the sValue ?
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: Lua Script tu update a device with json url -help

Post by Mazzokun »

yes, if I put
commandArray[1] = {['UpdateDevice'] = "45|0|100"}
works :roll:
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: Lua Script tu update a device with json url -help

Post by papoo »

Code: Select all

commandArray[1] = {['UpdateDevice'] = "45|0|" .. tostring(watt)}
?
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: Lua Script tu update a device with json url -help

Post 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 :?
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script tu update a device with json url -help

Post 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
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script tu update a device with json url -help

Post 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.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: Lua Script tu update a device with json url -help

Post 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
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script tu update a device with json url -help

Post 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
Last edited by simonrg on Sunday 22 May 2016 8:32, edited 1 time in total.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: Lua Script tu update a device with json url -help

Post 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)
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: Lua Script tu update a device with json url -help

Post by Mazzokun »

THIS SCRIPT WORKS :P :P :P :idea: :idea: :idea: :idea: :idea:

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 = {}

if otherdevices['Luci Cancellino'] == 'On'    then  watt1 = 50    else watt1 = 0 end
if otherdevices['Facciata'] == 'On'           then  watt2 = 21    else watt2 = 0 end
if otherdevices['Cupola Esterno'] == 'On'     then  watt3 = 8     else watt3 = 0 end
if otherdevices['Corridoio'] == 'On'          then  watt4 = 10    else watt4 = 0 end
if otherdevices['Cupola Xmas'] == 'On'        then  watt5 = 500   else watt5 = 0 end
if otherdevices['Cancellino Xmas'] == 'On'    then  watt6 = 400   else watt6 = 0 end
if otherdevices['Lampada Sala'] == 'On'       then  watt7 = 10    else watt7 = 0 end
if otherdevices['Faro Retro'] == 'On'         then  watt8 = 35    else watt8 = 0 end
if otherdevices['Faro Garage'] == 'On'        then  watt9 = 40    else watt9 = 0 end
if otherdevices['Lanterna Ingresso'] == 'On'  then  watt10 = 16   else watt10 = 0 end
if otherdevices['Lanterna Nonna'] == 'On'     then  watt11 = 25   else watt11 = 0 end
if otherdevices['Accento Ingresso'] == 'On'   then  watt12 = 8    else watt12 = 0 end
if otherdevices['Lampione'] == 'On'           then  watt13 = 23   else watt13 = 0 end
if otherdevices['Presa Volante'] == 'On'      then  watt14 = 300  else watt14 = 0 end
if otherdevices['Luce Garage'] == 'On'        then  watt15 = 4    else watt15 = 0 end

watt = 0+watt1+watt2+watt3+watt4+watt5+watt6+watt7+watt8+watt9+watt10+watt11+watt12+watt13+watt14+watt15

commandArray['UpdateDevice'] = "27|0|" .. tostring(watt)

return commandArray
THANK YOU ALL FOR SUPPORT :mrgreen:
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua Script tu update a device with json url -help

Post 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.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest