Page 1 of 1
CommandArray command in LUA doesn't work anymore
Posted: Wednesday 13 November 2024 21:59
by sebitop
Dear All
again this error happening either on Release or Beta version
I have a VIRTUAL SWITCH called DEBUG
I just run this code:
if (otherdevices['DEBUG']) == 'On' then
print('debug')
commandArray['DEBUG'] = 'Off'
end
Debug appears in the LOG
command array is not executed and the switch remains ON
any ideas ?
thansk again for your help !!
cheers
sebastien
Re: CommandArray command in LUA doesn't work anymore
Posted: Wednesday 13 November 2024 22:24
by jvdz
Is that your whole script?
Please show the total script.
Re: CommandArray command in LUA doesn't work anymore
Posted: Wednesday 13 November 2024 22:42
by sebitop
commandArray ={}
if (otherdevices['DEBUG']) == 'On' then
print('debug')
commandArray['DEBUG'] = 'Off'
end
return commandArray
it seems that my modifications on the meter calendar DB have an issue on this behavior as trying a previous day save seems to work but this does not explain why it does that
Re: CommandArray command in LUA doesn't work anymore
Posted: Wednesday 13 November 2024 22:49
by jvdz
That last line is wrong as the arrayname has a typo, so nothing is returned!
Re: CommandArray command in LUA doesn't work anymore
Posted: Wednesday 13 November 2024 22:54
by sebitop
it was a typo here
on my LUA the code is correct
Re: CommandArray command in LUA doesn't work anymore
Posted: Thursday 14 November 2024 8:07
by sebitop
I restored previous adatabase
It worked until this morning again the same issue....
Re: CommandArray command in LUA doesn't work anymore
Posted: Thursday 14 November 2024 9:19
by jvdz
Typo?
Just show us the real code, not something typed, doing a cut&paste so we can check the code for issues.

Re: CommandArray command in LUA doesn't work anymore
Posted: Thursday 14 November 2024 10:19
by sebitop
I'm trying to replicate the error finding which activation generates this issue as restoring a previous DB saved works well
nothing related to the domoticz version itself, behavior is the same with a stable or beta version
Do you have any idea of what could generate an issue preventing the commandArray command working on domoticz?
could it be linked to the database ?
I'm wondering as I have seen something this morning :
I have a script which works well since months and the aim is to retreive the color code of the french electricity (pricing is based on a color which changes every day)
this script parses an http address with JSON and extracts the color
but I realised that this morning the HTTP address was not reacheable showing an error message (error 403) but the JSON parsed some HTTP code and raised an error in the log
after I also realized that Domoticz was not working again
then I suspect this like an http injection things like tha
so now I'm searching what could have an impact in the DB that could stop CommadArray commands working
I need some additional tests to find exactly when it fails
I know this one is not an easy but I'm struggling to find out
thanks again for reading

Re: CommandArray command in LUA doesn't work anymore
Posted: Thursday 14 November 2024 12:05
by lost
sebitop wrote: ↑Thursday 14 November 2024 10:19
I have a script which works well since months and the aim is to retreive the color code of the french electricity (pricing is based on a color which changes every day)
this script parses an http address with JSON and extracts the color
but I realised that this morning the HTTP address was not reacheable showing an error message (error 403) but the JSON parsed some HTTP code and raised an error in the log
If you are using RTE Tempo data, server was unavailable this morning until ~8h45:
https://easydomoticz.com/forum/viewtopi ... 78#p122778
No issue on my side, as I do a quick check to see if answer starts with '{' as a JSON should, from the function that gets data:
Code: Select all
--
-- Internal fct :
-- Get json answer EDF or RTE http/json API.
--
local function getUrlJson(url)
config = assert(io.popen('curl --retry 1 --connect-timeout 2 -m 4 \"'..url..'\"'))
data = config:read('*all')
config:close()
-- Quick check answer is in JSON format
if data:sub(1, 1) == "{" then
jsonOut = json:decode(data)
else
jsonOut = nil
end
return(jsonOut)
end
Then if I get a 'nil' return a warning is logged and data is discarded (default values showing undefined day color/prices are used):
Code: Select all
-- Get all data for current tempo season
tempoSeasonURL = 'https://www.services-rte.com/cms/open_data/v1/tempo?season='..tempoSeason
tempoSeasonData = getUrlJson(tempoSeasonURL)
if (tempoSeasonData == nil) then
print("TEMPO : Warning, no data/color received from RTE ; Network issue?")
return tempoDay, tempoCount
end
That's not a foolproof check (the provided JSON.lua to handle this format does not provide such validation), but at least this avoids going ahead & depending on your data parsing code, the possibility (in my case, further checks exists anyway & not matching expected JSON format => no update) to feed DB with crap data through a virtual sensor for instance.
If you don't validate data & can feed crap to Domoticz, I'm afraid but your code is not "correct".
Re: CommandArray command in LUA doesn't work anymore
Posted: Thursday 14 November 2024 14:17
by sebitop
lost wrote: ↑Thursday 14 November 2024 12:05
If you don't validate data & can feed crap to Domoticz, I'm afraid but your code is not "correct".
It's probably the case and maybe this is what is causing the issue and blocking the CommandArray
the only things that changed since a couple of days is the RTE website being down
I will try to check this JSON like you did and restore domoticz from a previous working DB and see if it changes
thanks again for the code really helpful will test it
cheers
seb
Re: CommandArray command in LUA doesn't work anymore
Posted: Thursday 14 November 2024 22:03
by sebitop
Ok so after a couple of hours debugging here is the mystery but I found from where it comes
so first thanks for the JSON check, the issue is not coming from here but now the EDF JSON is perfectly parsed thanks again
now my problem :
I have a script called global from where I launch other scripts function of the time or other conditions, no issue so far
in this file there is a part of the code as following :
Code: Select all
if otherdevices[getdevicename(1631)] == 'On' then
dofile('/home/pi/domoticz/scripts/lua/script_chauffageauto.lua')
print('[ MESSAGE ] ► Gestion chauffage automatique ON')
else
print('[ MESSAGE ] ► Gestion chauffage automatique OFF')
end
the
script_chauffageauto.lua code is :
Code: Select all
commandArray={}
return commandArray
now here is the behavior :
- if the condition is ON and the dofile executed = CommandArray STOP WORKING
- if the condition is OFF the dofile is bypassed = commandarray WORKS
- if I remove the code from the script_chauffageauto.lua (so empty script) = commandarray WORKS
- if I rename script_chauffageauto.lua by script_time_chauffageauto.lua = Commandarray WORKS
- if I comment the dofile line --XXX = commandarray WORKS
I'm totally lost as some other dofile commands in the rest of the code are correctly executed without any issue
if you have any idea or want me to do some tests feel free to tell me
thanks !
seb
Re: CommandArray command in LUA doesn't work anymore
Posted: Thursday 14 November 2024 22:20
by jvdz
Ofcourse! The dofile needs to be looked at as putting the code from that file at the line of the dofile statement. This means you are clearing the commandArray variable at that time and end the script execution with the return in the dofile.
So do not use the commandArray = {} and return commandArray lines in the dofile!
Re: CommandArray command in LUA doesn't work anymore
Posted: Thursday 14 November 2024 22:25
by sebitop
Damn yes the position of this dofile was clearing the CommandArray table and this is why no command were executed after in domoticz
a so small thing !
thanks a million you saved my night

Re: CommandArray command in LUA doesn't work anymore
Posted: Wednesday 20 November 2024 13:50
by zicht
You could use something like :
Code: Select all
package.path = package.path .. ';' .. 'C:/PROGRA~2/domoticz/scripts/lua/functions.lua;'
local my1 = require ("functions")
at the firstrow in your script.
Then in the file functions.lua you could create as many functions as you want :
Code: Select all
function chauffage(var1, var 2, var3)
... do something with the var
return "Ok"
end
function anothersomething()
do something else....
end
this way you will be able to directly pass variables and call it as you need it like and ecen get returnvalues :
Code: Select all
x=chauffage("On","1234", "ABCD")
anothersomething()
This way your code and functions get separated and your code is nice and clean.
No need to worry for commandArray in your functions.lua file, as this is handled on the calling script.