Logic Machine Forum
Common script does not work in the way I want - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Common script does not work in the way I want (/showthread.php?tid=4307)



Common script does not work in the way I want - Hadeel - 20.10.2022

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



RE: Common script does not work in the way I want - admin - 21.10.2022

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
  }
})



RE: Common script does not work in the way I want - Hadeel - 25.10.2022

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 (: