Page 1 of 1

why does this simple script fail???

Posted: Friday 20 May 2022 19:12
by tiga
i am writing a script that needs to update 2 text devices with some energy values but it wont work.
so i wrote this simple test script and it fails allso.

Code: Select all

commandArray = {}

commandArray['UpdateDevice'] = 7705 ..'|0|'.. "why does it"
commandArray['UpdateDevice'] = 7704 ..'|0|'.. "not work?"

return commandArray
i run it on a time base.

but it only updates the second commandArray and skips the first.

how can i fix this?

Re: why does this simple script fail???

Posted: Friday 20 May 2022 20:25
by jvdz
tiga wrote: Friday 20 May 2022 19:12 but it only updates the second commandArray and skips the first.
Makes sense as LUA returns a Array variable (commandArray) and you are using the same key in the array so your array only contains one entry.... the last one. :)

Guess something like this will work.

Code: Select all

commandArray = {}
commandArray[7705] = 7705 ..'|0|'.. "why does it"
commandArray[7704] = 7704 ..'|0|'.. "not work?"
return commandArray

Re: why does this simple script fail???

Posted: Friday 20 May 2022 21:06
by tiga
this does not work.i get an error on this script

Re: why does this simple script fail???

Posted: Friday 20 May 2022 21:14
by tiga
maybe this makes more sense...this is the part of the origional script i am making that is going wrong.
it only updates the 7704 text device.

Code: Select all

if stroomverschil > 0 then
    commandArray['UpdateDevice'] = 7705 ..'|0|'..stroomverschil.. " kWh over "
end

if stroomverschil < 0 then
    commandArray['UpdateDevice'] = 7705 ..'|0|'..-stroomverschil.. " kWh tekort "
end


gas_nu = Round(gas_nu/1000, 0)
gas_verbruikt = gas_nu - gas_1jan
if gas_verbruikt > 0 then
commandArray['UpdateDevice'] = 7704 ..'|0|'..gas_verbruikt.. " m3h "
end

Re: why does this simple script fail???

Posted: Friday 20 May 2022 21:33
by jvdz
Yes that makes it clearer, and sorry about the missing updatedevice part previously.
try this version to allow for multiple UpdateDevice commands:

Code: Select all

if stroomverschil > 0 then
	commandArray['7705'] = { ['UpdateDevice'] = 7705 ..'|0|'..stroomverschil.. " kWh over " }
end

if stroomverschil < 0 then
	commandArray['7705'] = { ['UpdateDevice'] = 7705 ..'|0|'..-stroomverschil.. " kWh tekort " }
end


gas_nu = Round(gas_nu/1000, 0)
gas_verbruikt = gas_nu - gas_1jan
if gas_verbruikt > 0 then
	commandArray['7704'] = { ['UpdateDevice'] = 7704 ..'|0|'..gas_verbruikt.. " m3h " }
end
Jos :)

Re: why does this simple script fail???

Posted: Friday 20 May 2022 21:47
by tiga
it does not update either devce.
for the sake of testing i include a print command like this

Code: Select all

if stroomverschil > 0 then
    print('regel 44')
	commandArray['7705'] = { ['UpdateDevice'] = 7705 ..'|0|'..stroomverschil.. " kWh over " }
end

if stroomverschil < 0 then
    print('regel 49')
	commandArray['7705'] = { ['UpdateDevice'] = 7705 ..'|0|'..-stroomverschil.. " kWh tekort " }
end

gas_nu = Round(gas_nu/1000, 0)
gas_verbruikt = gas_nu - gas_1jan
if gas_verbruikt > 0 then
    print('regel 57')
	commandArray['7704'] = { ['UpdateDevice'] = 7704 ..'|0|'..gas_verbruikt.. " m3h " }
end
in the log regel 44 and regel 57 are showing up but no updates on the devices 7705 or 7704

Re: why does this simple script fail???

Posted: Friday 20 May 2022 21:56
by jvdz
You do have the line "return commandArray" at the end?

Re: why does this simple script fail???

Posted: Friday 20 May 2022 21:58
by tiga
yes it is in there at the bottom

Re: why does this simple script fail???

Posted: Friday 20 May 2022 22:03
by jvdz
.. or else try this version :

Code: Select all

if stroomverschil > 0 then
	commandArray[#commandArray + 1] = { ['UpdateDevice'] = 7705 ..'|0|'..stroomverschil.. " kWh over " }
end

if stroomverschil < 0 then
	commandArray[#commandArray + 1] = { ['UpdateDevice'] = 7705 ..'|0|'..-stroomverschil.. " kWh tekort " }
end

gas_nu = Round(gas_nu/1000, 0)
gas_verbruikt = gas_nu - gas_1jan
if gas_verbruikt > 0 then
	commandArray[#commandArray + 1] = { ['UpdateDevice'] = 7704 ..'|0|'..gas_verbruikt.. " m3h " }
end

Re: why does this simple script fail???

Posted: Friday 20 May 2022 22:08
by tiga
i dont know why my version does not work but this version of you works!!

thank you Jos!!