Page 1 of 1

global variables like otherdevices, otherdevices_lastupdate and otherdevices_svalues

Posted: Thursday 19 January 2017 16:20
by boe666
Hi.

I have a little problem witch sctipts located in lua_parses directory: I have no access to global tables like otherdevices, otherdevices_lastupdate and otherdevices_svalues. When i try do check some values in this tables, i have an error in log: (...) variable is nil.

I use this script to update values. My hardware call the spcial URL JSON.

Can you help me ?

Re: global variables like otherdevices, otherdevices_lastupdate and otherdevices_svalues

Posted: Thursday 19 January 2017 16:56
by Nautilus
The latest beta has these added, they are not on the stable I'm afraid...

Re: global variables like otherdevices, otherdevices_lastupdate and otherdevices_svalues

Posted: Friday 20 January 2017 8:48
by boe666
ok, so it's a normal situation in newest stable version or not ? In the new version (unstable for now) it's working ?

Re: global variables like otherdevices, otherdevices_lastupdate and otherdevices_svalues

Posted: Friday 20 January 2017 8:58
by Nautilus
Yes, more info e.g. here: viewtopic.php?p=115099

It's true that in some betas there might be issues, therefore I do not usually update immediately. But I've noticed there has been issues in the stable ones as well - at the time of release at least (fixed very soon of course). So, for the 2½ years I've been using Domoticz I cannot remember any specific issues arising from the fact that I've been on beta and not on stable. But yes, there's of course the risk (therefore daily full backups)...

Re: global variables like otherdevices, otherdevices_lastupdate and otherdevices_svalues

Posted: Monday 23 January 2017 16:39
by bellison
i can't tell why it is not working for you, it does for me.

test if this is a solution for you.

Code: Select all

 tAllDevicesById = {}
 tAllDevicesByName = {}
 function getMoreDeviceInformation()
    -- by calling this function you can get much more information about a device, eg the number of dim levels
    --[[
    Attributes available:
    AddjMulti; AddjMulti2; AddjValue; AddjValue2; BatteryLevel; CustomImage; Data; Description; Favorite; 
    HardwareID; HardwareName; HardwareType; HardwareTypeVal; HaveDimmer; HaveGroupCmd; HaveTimeout; ID; 
    Image; IsSubDevice; LastUpdate; Level; LevelInt; MaxDimLevel; Name; Notifications; PlanID; PlanIDs; 
    Protected; ShowNotifications; SignalLevel; Status; StrParam1; StrParam2; SubType; SwitchType; 
    SwitchTypeVal; Timers; Type; TypeImg; Unit; Used; UsedByCamera; XOffset; YOffset; idx
    --]]
    
    local sUrl = 'localhost'
    local sPort = '8082'
    
    local json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
    
    -- get all the device info needed, since it is not available in lua
    
    local config=assert(io.popen('curl http://'..sUrl..':'..sPort..'/json.htm?type=devices'))
    local jsonDevice = config:read('*all')
    config:close()
    local tDevices = json:decode(jsonDevice)
      
    -- put it in tables for easy access
    for k,v in pairs(tDevice['result']) do
        --print( "index : " .. k .. " = " .. v['idx'])
        tAllDevicesById[v['idx']] = v
        tAllDevicesByName[v['Name']] = v
    end 
end
sorry i misunderstood you did miss the lua_parses directory reference. but the solution might still be usable

Re: global variables like otherdevices, otherdevices_lastupdate and otherdevices_svalues

Posted: Tuesday 24 January 2017 14:20
by boe666
I tried... result: 2017-01-24 14:15:05.479 Error: CLuaHandler: /home/pi/domoticz/scripts/lua_parsers/d10.lua:27: attempt to index global 'tDevice' (a nil value).

I upgrade domoticz to latest version (unstable) and now it's ok. Example like this:
"function LogVariables(x,depth,name)
for k,v in pairs(x) do
if (depth>0) or ((string.find(k,'device')~=nil) or (string.find(k,'variable')~=nil) or.
(string.sub(k,1,4)=='time') or (string.sub(k,1,8)=='security')) then
if type(v)=="string" then print(name.."['"..k.."'] = '"..v.."'") end
if type(v)=="number" then print(name.."['"..k.."'] = "..v) end
if type(v)=="boolean" then print(name.."['"..k.."'] = "..tostring(v)) end
if type(v)=="table" then LogVariables(v,depth+1,k); end
end
end
end
-- call with this command
LogVariables(_G,0,'');
"
working very well.:)

Re: global variables like otherdevices, otherdevices_lastupdate and otherdevices_svalues

Posted: Tuesday 24 January 2017 15:33
by boe666
I have another problem.
My some harware send variables to domoticz by special URL with parameters - every 2 second.
So, when i want to monitoring movement detector i have on every 2 sec the same status.

First of all I want to check actual status before i write new status. I can do this now - i can read otherdevices_svalue and can make a decision to use domoticz_updateDevice procedure.

Code: Select all

tem = tonumber(alarm_ruch)
if (tem == 0 and otherdevices_svalues['Naruszenie strefy'] ~= 'Brak ruchu') then domoticz_updateDevice(9,1,'Brak ruchu') else end
if (tem == 1 and otherdevices_svalues['Naruszenie strefy'] ~= 'Wykryto Ruch!') then domoticz_updateDevice(9,4,'Wykryto Ruch!') else end

But domoticz after some time show me a red alert - no read on my movement detector.
How can I change a date in otherdevices_lastupdate ?
I tried:

Code: Select all

local now_time = os.date("%Y-%m-%d %H:%M:%S");
otherdevices_lastupdate['Naruszenie strefy'] = now_time;
but it's not working.

Re: global variables like otherdevices, otherdevices_lastupdate and otherdevices_svalues

Posted: Sunday 26 August 2018 20:16
by Melissen
@Bellison

I saw this LUA script with Json commands.
But how do I get a value (OFFSET) which I entered at a device (ADJUST/AANPASSEN and OFFSET)?

for example I entered an offset at device Watermeter (idx 403), offset is 1184308

How can I read out this value and put it in a value like 'watermeteroffset'? So I can use this value with: otherdevices/svalues['Watermeter'] to get the total amount.

Hope you understand my problem


best regards,
Hein