19.02.2018, 15:39
(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.