Logic Machine Forum
Trends API - 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: Trends API (/showthread.php?tid=313)

Pages: 1 2


RE: Trends API - Trond Hoyem - 17.06.2022

(17.06.2022, 13:23)admin Wrote: Not 120 but 127.0.0.1

You are right. The typo is only in the previous post, the address in the script is 127.0.0.1.


RE: Trends API - admin - 17.06.2022

The error comes from having @ symbol in the password. It confuses the URL parser. Try using a password without @.


RE: Trends API - Trond Hoyem - 17.06.2022

(17.06.2022, 13:30)admin Wrote: The error comes from having @ symbol in the password. It confuses the URL parser. Try using a password without @.

Yes, I saw that as well, so I have changed the password so that there is no @ any more. 

I now get "timeout" in stead of "host not found"


RE: Trends API - admin - 17.06.2022

The IP blocker might have blocked local access because of wrong password. Reboot the device and try again.


RE: Trends API - Trond Hoyem - 17.06.2022

(17.06.2022, 13:34)admin Wrote: The IP blocker might have blocked local access because of wrong password. Reboot the device and try again.

I have also tried that. I actually down-powered it and repowered. Still same timeout.


RE: Trends API - admin - 17.06.2022

What do you get logged from this script?
Code:
require('socket.http')
log(socket.http.request('http://127.0.0.1/'))



RE: Trends API - Trond Hoyem - 17.06.2022

(17.06.2022, 14:15)admin Wrote: What do you get logged from this script?
Code:
require('socket.http')
log(socket.http.request('http://127.0.0.1/'))

Here is the whole script: 
Code:
require('json')
require('socket.url')
require('socket.http')

user = 'admin'
Password = 'Ringshusveien_18'
tag = 'nyTrend'

-- parameters
daily = 365*24*60 -- 1 day
daily1year = 365*24*12 -- 365 day
daily2year = 2*365*24*12 -- 365 day
year5_d = 5*365 -- 5 year


obj = grp.tag(tag)


for _, addr in ipairs(obj) do

  object = addr.address
  alias = addr.name

  trend = {
    object = knxlib.encodega(object), -- object id in db
    name = alias, -- trend name
    type = trend_type,
    resolution = 5, -- every 5 minute
    precision = 2, -- floating point precision
    count_resolution = 365*24*12, -- 1 year
    count_daily = 5*365 -- 5 years
  }

  data = json.encode(trend)
  post = 'data=' .. socket.url.escape(data)
  grp.removetags(addr.name, tag)

  res, err = socket.http.request('http://' .. user .. ':' .. password .. '@127.0.0.1/scada-main/trends/save', post)
  log(res, err)

end


script.disable(_SCRIPTNAME)


I am basically logging the result and errors from the saving of the trend.


RE: Trends API - admin - 17.06.2022

Works for me. Try running script from my previous post and see what you get in Logs.
Your script is also missing trend_type variable. It should be either 'C', 'D' or 'G' (counter, counter with negative delta or absolute value respectively).


RE: Trends API - Domizy - 18.12.2024

(19.02.2018, 15:39)Domoticatorino Wrote:
(07.12.2016, 20:15)Pawel Wrote: Thanks Erwin, those scripts save me a lot of time. I made a small change and all objects with specific tag will have own trend: 

Code:
-- Batch adding trends - Created by Erwin van der Zwart - Schneider Electric Netherlands -----------
-- For spaceLYnk FW 1.2.1 or higher and homeLYnk FW 1.5.1 or higher --------------------------------
-- Refresh browser after creation to show the trends -----------------------------------------------
----------------------------------------- Start Parameters -----------------------------------------




-- Create trend to object with this tag 
tag_name = "make_trend"

-- Set username and password for access to HL
username = 'admin'
password = 'admin'

-- Select trend type to create (or multiple types on same object)
create_trendtype_counter = true
create_trendtype_counter_with_negative_delta = true
create_trendtype_absolute_value = true

-- Set trend precicion (!Important! Can only be these values: 0 to 8)
trendprecision = 0

-- Set trend resolution (!Important! Can only be these values: 5 / 10 / 15 / 20 / 30 / 60 minutes))
trendresolution = 15

-- Set trend count resolution (!Important! Can only be these values: '30' = 30 days, '180' = 180 days, '365' = 1 year, '730' = 2 years, '1825' = 5 years
trendcountresolution = 365

-- Set trend count days (!Important! Can only be these values: '1' = 1 year / '2' = 2 years / '5' = 5 years / '10' = 10 years)
trendcountdaily = 10

-- Set show always 0 base line 
trendshowzero = 0 -- 0 = disabled 1 = enabled

------------------------------------------ End Parameters ------------------------------------------
------------------------------ DON'T CHANGE ANYTHING UNDER THIS LINE -------------------------------
-- Load modules
require('json')
require('socket.url')
require('socket.http')

-- Set HL ip address as localhost
ip = '127.0.0.1'


-- Create url for trend creation
url = 'http://' .. username .. ':' .. password .. '@' .. ip .. '/scada-main/trends/save'

-- Function to send request to create trend
function url_send(trend_object, trend_name, trend_type, trend_resolution, trend_precision, trend_count_resolution, trend_count_daily, trend_show_zero, trend_id)
  local trend = {
      object = trend_object,
      name = trend_name,
      type = trend_type,
      resolution = trend_resolution,
      precision = trend_precision,
    count_resolution = trend_count_resolution * 12 * 24, -- nr of days * nr of points in a hour at min resolution (60/5 = 12) * hours per day
    count_daily = trend_count_daily * 365, -- nr of days * 365 days a year
      show_zero = trend_show_zero,
      id = trend_id,
    }
  data = json.encode(trend)
    form_data = 'data=' .. socket.url.escape(data)
    socket.http.TIMEOUT = 15   
    local res, code, response_header = socket.http.request(url, form_data)
  return res, code, response_header
end

-- Set counters for creation log
number_of_trends = 0
number_failed = 0
objectwithtag = grp.tag(tag_name)
-- Loop for each group adress with specific tag 

for key, value in pairs(objectwithtag) do
  
  -- Get current group address from loop 
  current_GA = knxlib.decodega(value.id)
  
  -- Get current object info
  objectinfo = grp.find(current_GA)
    
  -- Check if object excists
  if objectinfo ~= nil then
      
    -- Set parameters for request
    trendobject = objectinfo.id
    trendname = objectinfo.name
    trendid = ''
    
    -- Check if trend type 'Counter' should be created
    if create_trendtype_counter == true then
      
      -- Call function to send create request
      result, code, response_header = url_send(trendobject, trendname, 'C', trendresolution, trendprecision, trendcountresolution, trendcountdaily, trendshowzero, trendid)
      
      -- Calculate results for creation results log
        if result == '{"success":true}' then
          number_of_trends = number_of_trends + 1
        else
          number_failed = number_failed + 1
        end
      
    end
    
    -- Check if trend type 'Counter with negative delta' should be created
    if create_trendtype_counter_with_negative_delta == true then
      
            -- Call function to send create request
      result, code, response_header = url_send(trendobject, trendname, 'D', trendresolution, trendprecision, trendcountresolution, trendcountdaily, trendshowzero, trendid)
          
      -- Calculate results for creation results log
        if result == '{"success":true}' then
          number_of_trends = number_of_trends + 1
        else
          number_failed = number_failed + 1
        end
      
    end
    
    -- Check if trend type 'Absolute value' should be created
    if create_trendtype_absolute_value == true then
            
      -- Call function to send create request
      result, code, response_header = url_send(trendobject, trendname, 'G', trendresolution, trendprecision, trendcountresolution, trendcountdaily, trendshowzero, trendid)
          
      -- Calculate results for creation results log
        if result == '{"success":true}' then
          number_of_trends = number_of_trends + 1
        else
          number_failed = number_failed + 1
        end
      
    end
    
  end
  
end
    
if number_failed == 0 then
    log ("Created " .. number_of_trends .. " trends succesfully")
else
    log ("Created " .. number_of_trends .. " trends succesfully and creation of " .. number_failed .. " trends failed")
end

-- Disable script when done automaticly
script.disable(_SCRIPTNAME)

Hi Pawel,
in this case don't you create .csv file?
Thanks.

Hello,

I have just carried out a test with this script which seems clear and complete to me. But it doesn't work for me?! I linked the Tag to three variables for the test and the log return is as follows: * string: Created 0 trends successfully and creation of 4 trends failed. Do you have any possible solutions?


RE: Trends API - Daniel - 18.12.2024

Which controller and what firmware do you use?


RE: Trends API - Domizy - 18.12.2024

(18.12.2024, 14:51)Daniel Wrote: Which controller and what firmware do you use?

Schneider LSS100200 (Spacelynk), firmware 3.0.0


RE: Trends API - Daniel - 18.12.2024

Did you disable: Block unsafe functions in scripts: in general configuration?


RE: Trends API - Domizy - 18.12.2024

(18.12.2024, 15:29)Daniel Wrote: Did you disable: Block unsafe functions in scripts: in general configuration?

I tried with and without the function, the result is the same.


RE: Trends API - Erwin van der Zwart - 19.12.2024

Can you try this version, it uses webrequests instead of URL with basic authentication that is not supported anymore in FW 3.0.0 
Code:
-- Batch adding trends v1.3 - Created by Erwin van der Zwart - Schneider Electric Netherlands -------------------
-- For spaceLYnk FW 3.0.0 or higher and Wiser for KNX FW 3.0.0 or higher ----------------------------------------
-- Make sure that "Block unsafe functions in scripts" is disbled in General Configuration
-- Refresh browser after creation to show the trends ------------------------------------------------------------
----------------------------------------------- Start Parameters ------------------------------------------------

-- Start address of objects
start_objectaddress = '3/0/0'

-- Trends for all objects between these range are created

-- End address of objects
end_objectaddress = '3/0/2'

-- Select trend type to create (or multiple types on same object)
create_trendtype_counter = false
create_trendtype_counter_with_negative_delta = false
create_trendtype_absolute_value = true

-- Select aggregate function (!Important! Can only be these values: AVERAGE / MIN / MAX / LAST))
trendaggregatefunction = 'AVERAGE'

-- Set trend resolution (!Important! Can only be these values: 5 / 10 / 15 / 20 / 30 / 60 minutes))
trendresolution = 5

-- Set trend precicion (!Important! Can only be these values: 0 to 8) decimal places
trendprecision = 2

-- Set trend count resolution (!Important! Can only be these values: '30' = 30 days, '180' = 180 days, '365' = 1 year, '730' = 2 years, '1825' = 5 years
trendcountresolution = 365

-- Set trend count days (!Important! Can only be these values: '1' = 1 year / '2' = 2 years / '5' = 5 years / '10' = 10 years)
trendcountdaily = 10

-- Set show always 0 base line
trendshowzero = 0 -- 0 = disabled 1 = enabled

------------------------------------------------ End Parameters -------------------------------------------------
------------------------------------ DON'T CHANGE ANYTHING UNDER THIS LINE --------------------------------------

-- Calculate start address to DB format
start_objectaddress = knxlib.encodega(start_objectaddress)

-- Calculate end address to DB format
end_objectaddress = knxlib.encodega(end_objectaddress)

-- Function to send request (in this case to create a trend)
function webrequest(mod, act, vars, data)
  require('json')
  require('dbenv')
  local path
  vars = vars or {}
  function getvar(v)
      return vars[ v ]
  end
  json.data = function()
      return data or {}
  end
  if mod == 'plugin' then
  path = 'plugins/' .. act .. '/web.lua'
  else
   path = 'web/' .. mod .. '/' .. act .. '.lua'
  end
  result = dofile('/lib/genohm-scada/' .. path)
  -- Calculate results for creation results log
  if result.success == true then
    number_of_trends = number_of_trends + 1
  else
    number_failed = number_failed + 1
  end
end

-- Function to create vars
function createvars(trend_object, trend_name, trend_type, trend_aggregate_function, trend_resolution, trend_precision, trend_count_resolution, trend_count_daily, trend_show_zero, trend_id)
  local vars = {
      object = trend_object,
      name = trend_name,
      type = trend_type,
      aggregation = trend_aggregate_function,
      resolution = trend_resolution,
      precision = trend_precision,
    count_resolution = trend_count_resolution * 12 * 24, -- nr of days * nr of points in a hour at min resolution (60/5 = 12) * hours per day
    count_daily = trend_count_daily * 365, -- nr of days * 365 days a year
      show_zero = trend_show_zero,
      id = trend_id,
    }
  return vars
end

-- Set counters for creation log
number_of_trends = 0
number_failed = 0

-- Loop from startadres to end address to create trends
for i = start_objectaddress, end_objectaddress, 1 do
 
  -- Get current group address from loop
  current_GA = knxlib.decodega(i)
 
  -- Get current object info
  objectinfo = grp.find(current_GA)
    
  -- Check if object excists
  if objectinfo ~= nil then
      
    -- Set parameters for request
    trendobject = objectinfo.id
    trendname = objectinfo.name
    trendid = ''
   
    -- Check if trend type 'Counter' should be created
    if create_trendtype_counter == true then
      -- Call function to create vars
      requestdata = createvars(trendobject, trendname, 'C', trendaggregatefunction, trendresolution, trendprecision, trendcountresolution, trendcountdaily, trendshowzero, trendid)
      webrequest('trends', 'save', {request = 'save'}, requestdata)
    end
   
    -- Check if trend type 'Counter with negative delta' should be created
    if create_trendtype_counter_with_negative_delta == true then
            -- Call function to send create vars
      requestdata = createvars(trendobject, trendname, 'D', trendaggregatefunction, trendresolution, trendprecision, trendcountresolution, trendcountdaily, trendshowzero, trendid)
      webrequest('trends', 'save', {request = 'save'}, requestdata)
    end
   
    -- Check if trend type 'Absolute value' should be created
    if create_trendtype_absolute_value == true then
            -- Call function to send create vars
      requestdata = createvars(trendobject, trendname, 'G', trendaggregatefunction, trendresolution, trendprecision, trendcountresolution, trendcountdaily, trendshowzero, trendid)
      webrequest('trends', 'save', {request = 'save'}, requestdata)
    end
   
  end
 
end
   
if number_failed == 0 then
    log ("Created " .. number_of_trends .. " trends succesfully")
else
    log ("Created " .. number_of_trends .. " trends succesfully and creation of " .. number_failed .. " trends failed")
end

-- Disable script when done automaticly
script.disable(_SCRIPTNAME)



RE: Trends API - Domizy - 19.12.2024

(19.12.2024, 14:57)Erwin van der Zwart Wrote: Can you try this version, it uses webrequests instead of URL with basic authentication that is not supported anymore in FW 3.0.0 
Code:
-- Batch adding trends v1.3 - Created by Erwin van der Zwart - Schneider Electric Netherlands -------------------
-- For spaceLYnk FW 3.0.0 or higher and Wiser for KNX FW 3.0.0 or higher ----------------------------------------
-- Make sure that "Block unsafe functions in scripts" is disbled in General Configuration
-- Refresh browser after creation to show the trends ------------------------------------------------------------
----------------------------------------------- Start Parameters ------------------------------------------------

-- Start address of objects
start_objectaddress = '3/0/0'

-- Trends for all objects between these range are created

-- End address of objects
end_objectaddress = '3/0/2'

-- Select trend type to create (or multiple types on same object)
create_trendtype_counter = false
create_trendtype_counter_with_negative_delta = false
create_trendtype_absolute_value = true

-- Select aggregate function (!Important! Can only be these values: AVERAGE / MIN / MAX / LAST))
trendaggregatefunction = 'AVERAGE'

-- Set trend resolution (!Important! Can only be these values: 5 / 10 / 15 / 20 / 30 / 60 minutes))
trendresolution = 5

-- Set trend precicion (!Important! Can only be these values: 0 to 8) decimal places
trendprecision = 2

-- Set trend count resolution (!Important! Can only be these values: '30' = 30 days, '180' = 180 days, '365' = 1 year, '730' = 2 years, '1825' = 5 years
trendcountresolution = 365

-- Set trend count days (!Important! Can only be these values: '1' = 1 year / '2' = 2 years / '5' = 5 years / '10' = 10 years)
trendcountdaily = 10

-- Set show always 0 base line
trendshowzero = 0 -- 0 = disabled 1 = enabled

------------------------------------------------ End Parameters -------------------------------------------------
------------------------------------ DON'T CHANGE ANYTHING UNDER THIS LINE --------------------------------------

-- Calculate start address to DB format
start_objectaddress = knxlib.encodega(start_objectaddress)

-- Calculate end address to DB format
end_objectaddress = knxlib.encodega(end_objectaddress)

-- Function to send request (in this case to create a trend)
function webrequest(mod, act, vars, data)
  require('json')
  require('dbenv')
  local path
  vars = vars or {}
  function getvar(v)
      return vars[ v ]
  end
  json.data = function()
      return data or {}
  end
  if mod == 'plugin' then
  path = 'plugins/' .. act .. '/web.lua'
  else
   path = 'web/' .. mod .. '/' .. act .. '.lua'
  end
  result = dofile('/lib/genohm-scada/' .. path)
  -- Calculate results for creation results log
  if result.success == true then
    number_of_trends = number_of_trends + 1
  else
    number_failed = number_failed + 1
  end
end

-- Function to create vars
function createvars(trend_object, trend_name, trend_type, trend_aggregate_function, trend_resolution, trend_precision, trend_count_resolution, trend_count_daily, trend_show_zero, trend_id)
  local vars = {
      object = trend_object,
      name = trend_name,
      type = trend_type,
      aggregation = trend_aggregate_function,
      resolution = trend_resolution,
      precision = trend_precision,
    count_resolution = trend_count_resolution * 12 * 24, -- nr of days * nr of points in a hour at min resolution (60/5 = 12) * hours per day
    count_daily = trend_count_daily * 365, -- nr of days * 365 days a year
      show_zero = trend_show_zero,
      id = trend_id,
    }
  return vars
end

-- Set counters for creation log
number_of_trends = 0
number_failed = 0

-- Loop from startadres to end address to create trends
for i = start_objectaddress, end_objectaddress, 1 do
 
  -- Get current group address from loop
  current_GA = knxlib.decodega(i)
 
  -- Get current object info
  objectinfo = grp.find(current_GA)
    
  -- Check if object excists
  if objectinfo ~= nil then
      
    -- Set parameters for request
    trendobject = objectinfo.id
    trendname = objectinfo.name
    trendid = ''
   
    -- Check if trend type 'Counter' should be created
    if create_trendtype_counter == true then
      -- Call function to create vars
      requestdata = createvars(trendobject, trendname, 'C', trendaggregatefunction, trendresolution, trendprecision, trendcountresolution, trendcountdaily, trendshowzero, trendid)
      webrequest('trends', 'save', {request = 'save'}, requestdata)
    end
   
    -- Check if trend type 'Counter with negative delta' should be created
    if create_trendtype_counter_with_negative_delta == true then
            -- Call function to send create vars
      requestdata = createvars(trendobject, trendname, 'D', trendaggregatefunction, trendresolution, trendprecision, trendcountresolution, trendcountdaily, trendshowzero, trendid)
      webrequest('trends', 'save', {request = 'save'}, requestdata)
    end
   
    -- Check if trend type 'Absolute value' should be created
    if create_trendtype_absolute_value == true then
            -- Call function to send create vars
      requestdata = createvars(trendobject, trendname, 'G', trendaggregatefunction, trendresolution, trendprecision, trendcountresolution, trendcountdaily, trendshowzero, trendid)
      webrequest('trends', 'save', {request = 'save'}, requestdata)
    end
   
  end
 
end
   
if number_failed == 0 then
    log ("Created " .. number_of_trends .. " trends succesfully")
else
    log ("Created " .. number_of_trends .. " trends succesfully and creation of " .. number_failed .. " trends failed")
end

-- Disable script when done automaticly
script.disable(_SCRIPTNAME)


It works perfectly!  thank you very much for the time saved on creating the graphics Smile