This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Common script does not work in the way I want
#1
I have a function called "commpnPost()" in a common script as below.

I call this function everytime when there's a change in each KNX status object, so that I can let other servers know that there's a status change in the KNX project. 
I call it in each event script as the image below.   ('9/2/1' is the group address of the status object)
   

However, when I log the output inside the common script, I can see the first argument '9/2/1' somehow changed to '10/4/5'.
   
I also made sure that it already changed to '10/4/5' at the first line of the commonPost function.

This happens with other group addresses too.
Am I using common script in a wrong way ? Or maybe there is a chace that I need to clear inside LM ?


Code:
function commonPost(add, value)
  local dptString  = convertToDptString(add)
  local payload = ""
  if dptString == "dtDate" then
    payload = '{"groupAddress":  "' ..add..'","value": {"dtDate": { "year":' ..tostring(value["year"]).. ', "month": '..tostring(value["month"])..  ', "day": '..tostring(value["day"]).. '}}}'
  elseif dptString == "dtTime" then
    payload = '{"groupAddress":  "' ..add..'","value": {"dtTime": { "hour":' ..tostring(value["hour"]).. ', "minute": '..tostring(value["minute"])..  ', "second": '..tostring(value["second"]).. '}}}'
  else
    payload = '{"groupAddress":  "' ..add..'","value": {"'..dptString.. '": ' ..tostring(value).. '}}'
  end
  log('payload: ', payload)
  --**post the payload to a speficic REST endpoint  **
end

Code:
function convertToDptString(address)
  mapping = {}
  for key, value in pairs(dt) do
    mapping[ value ] = 'dt' .. string.upper(string.sub(key, 1, 1)) .. string.sub(key, 2)
  end
  obj = grp.find(address)
  dpt = obj.datatype
  if dpt then
  dtname = mapping[ dpt ]

  if not dtname and dpt >= 1000 then
    dtname = mapping[ math.floor(dpt / 1000) ]
  end
end
  return dtname
end
Reply
#2
Check if you have duplicate function somewhere else.

You don't have to specify the address manually, use event.dst instead. This way you can map a single event script to a tag instead of creating many event scripts with the same functionality.

Don't create JSON strings manually, use JSON library:
Code:
local payload = require('json').encode({
  groupAddress = add,
  value = {
    [ dptString ] = value
  }
})
Reply
#3
Dear Admin,

Thank you for your reply and for your code !

I checked if there was a duplicate function but no....and we tried a factory reset after taking a backup.
After the factory reset I uploaded a backup file and everything is working fine now!

Probably because the object structure has changed a lot after installing the LM projecet and we uploaded ets project files for few times without deleting the object....maybe the object was linked to the old script ?

I started to use event.dst also (:

Thank you for your help always (:
Reply


Forum Jump: