why does this simple script fail???

Moderator: leecollings

Post Reply
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

why does this simple script fail???

Post 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?
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: why does this simple script fail???

Post 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
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: why does this simple script fail???

Post by tiga »

this does not work.i get an error on this script
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: why does this simple script fail???

Post 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
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: why does this simple script fail???

Post 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 :)
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: why does this simple script fail???

Post 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
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: why does this simple script fail???

Post by jvdz »

You do have the line "return commandArray" at the end?
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: why does this simple script fail???

Post by tiga »

yes it is in there at the bottom
User avatar
jvdz
Posts: 2441
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: why does this simple script fail???

Post 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
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: why does this simple script fail???

Post by tiga »

i dont know why my version does not work but this version of you works!!

thank you Jos!!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest