Script only running the second command Topic is solved

Moderator: leecollings

Post Reply
DavidDavid
Posts: 10
Joined: Wednesday 22 March 2017 1:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Script only running the second command

Post by DavidDavid »

I have a script that I didn't write (stole the bones from github and modified to my needs) and it's working, but not perfectly.


Code: Select all

    time = os.date("*t")
mins = time.min + time.hour * 60
commandArray = {} 
libs = require("libs")

-- Coop Temp OSD
Coop = otherdevices_temperature['Coop Temp']*9/5+32
Coop = libs.round(Coop,0)
if (Coop ~= tonumber(uservariables["Coop"])) then
        commandArray["Variable:Coop"]=tostring(Coop)	
        commandArray['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.55/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].Text=' .. tostring(Coop) .. '°F'
	print('Updating Coop Temp OSD')
end

-- Run Temp OSD
Run = otherdevices_temperature['Coop Temp']*9/5+32
Run = libs.round(Run,0)
if (Run ~= tonumber(uservariables["Run"])) then
        commandArray["Variable:Run"]=tostring(Run)	
        commandArray['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.56/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].Text=' .. tostring(Run) .. '°F'
	print('Updating Run Temp OSD')
end

-- Day/Night Video Profile Switching
if (mins >= timeofday['SunsetInMinutes']) and (tonumber(uservariables["ODCam-Profile"]) ~= 1) then
	print("Switching Outdoor Cameras to Night Profile.")
	commandArray[1]={ ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.52/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=3' }
	commandArray[2]={ ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.53/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=3' }
	commandArray[3]={ ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.54/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=3' }
	commandArray[4]={ ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.55/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=3' }
    commandArray[5]={ ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.56/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=3' }
    commandArray[6]={ ["Variable:ODCam-Profile"] = "1" }
elseif (mins >= timeofday['SunriseInMinutes']) and (tonumber(uservariables["ODCam-Profile"]) ~= 0) and (mins < timeofday['SunsetInMinutes']) then
	print("Switching Outdoor Cameras to Day Profile.")
	commandArray[1]={ ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.52/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=0' }
	commandArray[2]={ ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.53/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=0' }
	commandArray[3]={ ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.54/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=0' }
	commandArray[4]={ ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.55/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=0' }
    commandArray[5]={ ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.56/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode=0' }
    commandArray[6]={ ["Variable:ODCam-Profile"] = "0" }
end

return commandArray    
The first two commands take the temperature reading from a sensor I have in my network and add that to the camera feed as a text overlay. The last set automatically switch my cameras from day to night profile at sunrise/sunset.

Everything works great, except the only text overlay command that works is whatever is Second. If I comment out either one, the other one works just fine. If I swap positions, the second one works, so I know the actual code is good.

Any ideas as to how I can get both temperature commands to run? This particular issue only affects the temp commands. The profile switching works no matter what, even though it's last in line.

Thanks
alanlsmith
Posts: 132
Joined: Monday 07 August 2017 17:17
Target OS: Linux
Domoticz version: Latest β
Location: Near London (Saaarf!)
Contact:

Re: Script only running the second command

Post by alanlsmith »

Just change it to:
commandArray[#commandArray+1] = {["Variable:Coop"]=tostring(Coop)}
commandArray[#commandArray+1] = {['OpenURL'] = uservariables['camlogin']}
or
commandArray[1] = {["Variable:Coop"]=tostring(Coop)}
commandArray[2] = {['OpenURL'] = uservariables['camlogin']}

If you just use commandArray each time the last one overrides the previous ones.
Domoticz Latest β, RPi 4B with 110Gb SSD for Domoticz, RPi 4B with 110Gb SSD for Node-Red & a RPi 2B for logging / IP addressing. RFXCOM, PiZiGate, Z-Wave, Harmony, Hue lamps and a bit of Broadlink.
DavidDavid
Posts: 10
Joined: Wednesday 22 March 2017 1:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script only running the second command

Post by DavidDavid »

Not sure what you mean by it over riding the previous ones if I just use commandArray. The code I have IS working individually, they just aren't working together.

I tried changing it to this and it isn't working at all now....

Code: Select all

-- Coop Temp OSD
Coop = otherdevices_temperature['Coop Temp']*9/5+32
Coop = libs.round(Coop,0)
if (Coop ~= tonumber(uservariables["Coop"])) then
        commandArray[1] = { ["Variable:Coop"]=tostring(Coop) }	
        commandArray[2] = { ['OpenURL'] = uservariables['camlogin']  .. '@192.168.1.55/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].Text=' .. tostring(Coop) .. '55°F' }
	print('Updating Coop Temp OSD')
end

-- Run Temp OSD
Run = otherdevices_temperature['Coop Temp']*9/5+32
Run = libs.round(Run,0)
if (Run ~= tonumber(uservariables["Run"])) then
        commandArray[1] = { ["Variable:Run"]=tostring(Run) }
        commandArray[2] = { ['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.56/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].Text=' .. tostring(Run) .. '55°F' }
	print('Updating Run Temp OSD')
end
Logread
Posts: 228
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: Script only running the second command

Post by Logread »

@DavidDavid,

like @alanlsmith explained, if you overwrite the same table index (e.g. 'OpenURL') multiple times, only the last assignment will be retained... you need to add one dimension to the table like suggested but it is easier in a dynamic way, either using commandArray[#commandArray+1] all the way for ALL assignments or like I prefer the construct based on table.insert()... the code you show in your last post overwrites commandArray[1] and commandArray[2]...
The following code might work for you (it is syntactically correct, but I obviously cannot test it for you):

Code: Select all

time = os.date("*t")
mins = time.min + time.hour * 60
commandArray = {} 
libs = require("libs")
cmdurl = "/cgi-bin/configManager.cgi?action=setConfig&VideoInOptions[0].NightOptions.SwitchMode="

-- Coop Temp OSD
Coop = otherdevices_temperature['Coop Temp']*9/5+32
Coop = libs.round(Coop,0)
if (Coop ~= tonumber(uservariables["Coop"])) then
        table.insert(commandArray, {["Variable:Coop"] = tostring(Coop)})
        table.insert(commandArray, {['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.55/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].Text=' .. tostring(Coop) .. '°F'})
	print('Updating Coop Temp OSD')
end

-- Run Temp OSD
Run = otherdevices_temperature['Coop Temp']*9/5+32
Run = libs.round(Run,0)
if (Run ~= tonumber(uservariables["Run"])) then
        table.insert(commandArray, {["Variable:Run"]=tostring(Run)})	
        table.insert(commandArray, {['OpenURL'] = uservariables['camlogin'] .. '@192.168.1.56/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].Text=' .. tostring(Run) .. '°F'})
	print('Updating Run Temp OSD')
end

-- Day/Night Video Profile Switching
if (mins >= timeofday['SunsetInMinutes']) and (tonumber(uservariables["ODCam-Profile"]) ~= 1) then
	print("Switching Outdoor Cameras to Night Profile.")
	table.insert(commandArray, {['OpenURL'] = table.concat({uservariables['camlogin'], "@192.168.1.52", cmdurl, '3' })})
	table.insert(commandArray, {['OpenURL'] = table.concat({uservariables['camlogin'], "@192.168.1.53", cmdurl, '3' })})
	table.insert(commandArray, {['OpenURL'] = table.concat({uservariables['camlogin'], "@192.168.1.54", cmdurl, '3' })})
	table.insert(commandArray, {['OpenURL'] = table.concat({uservariables['camlogin'], "@192.168.1.55", cmdurl, '3' })})
 	table.insert(commandArray, {['OpenURL'] = table.concat({uservariables['camlogin'], "@192.168.1.56", cmdurl, '3' })})
 	table.insert(commandArray, {["Variable:ODCam-Profile"] = "1" })
elseif (mins >= timeofday['SunriseInMinutes']) and (tonumber(uservariables["ODCam-Profile"]) ~= 0) and (mins < timeofday['SunsetInMinutes']) then
	print("Switching Outdoor Cameras to Day Profile.")
	table.insert(commandArray, {['OpenURL'] = table.concat({uservariables['camlogin'], "@192.168.1.52", cmdurl, '0' })})
	table.insert(commandArray, {['OpenURL'] = table.concat({uservariables['camlogin'], "@192.168.1.53", cmdurl, '0' })})
	table.insert(commandArray, {['OpenURL'] = table.concat({uservariables['camlogin'], "@192.168.1.54", cmdurl, '0' })})
	table.insert(commandArray, {['OpenURL'] = table.concat({uservariables['camlogin'], "@192.168.1.55", cmdurl, '0' })})
  	table.insert(commandArray, {['OpenURL'] = table.concat({uservariables['camlogin'], "@192.168.1.56", cmdurl, '0' })})
  	table.insert(commandArray, {["Variable:ODCam-Profile"] = "0" })
end

return commandArray
note that I also changed some of the terrible string concatenation instances for the cleaner and much more memory and resources efficient table.concat()
hope this helps
DavidDavid
Posts: 10
Joined: Wednesday 22 March 2017 1:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script only running the second command

Post by DavidDavid »

@Logread - You rock. That worked perfectly. And thank you for the tip about the cmdurl bit you added.

I clearly don't know how to code any of this so thank you for taking the time write it out!
alanlsmith
Posts: 132
Joined: Monday 07 August 2017 17:17
Target OS: Linux
Domoticz version: Latest β
Location: Near London (Saaarf!)
Contact:

Re: Script only running the second command

Post by alanlsmith »

@DavidDavid

Just for information your amended script (the one that wasn't working at all) was just missing: return commandArray at the end of the script.
Domoticz Latest β, RPi 4B with 110Gb SSD for Domoticz, RPi 4B with 110Gb SSD for Node-Red & a RPi 2B for logging / IP addressing. RFXCOM, PiZiGate, Z-Wave, Harmony, Hue lamps and a bit of Broadlink.
DavidDavid
Posts: 10
Joined: Wednesday 22 March 2017 1:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script only running the second command

Post by DavidDavid »

It had it in there at the end of the whole thing (after the day/night profile switching) but I just copied and pasted the stuff I was working on....
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest