Write Python scripts
With the introduction of Eliona Version 12.2, important changes were made that require scripts to be adapted in order to use the new API design.
Script definition
When a new script is created in the Eliona system, a function header is generated automatically. This header contains two parameters:
id: The unique ID of the script.eliona: An object through which the user can interact with the Eliona system.
The function header looks as follows:
# you may add imports only inside the function
# don't change or delete the function definition
def UserFunction(id, eliona):
# add your code hereImporting modules
All necessary modules must be imported within the function. It is important that modules are imported only within the function and that no global imports are used.
Example:
def UserFunction(id, eliona):
import random
# More lines of code followMethods of the Eliona object
The eliona-object provides a range of methods that enable users to interact with the Eliona system. Here are the most important methods together with their parameters and descriptions:
GetHeap
gai: str, subtype: str, attribute: str
Retrieves the current value of an attribute or the entire data JSON of an asset from the heap.
SetHeap
gai: str, subtype: str, data: dict, source: str
Sends data to the Calculator to process it and store it in the heap.
GetAssetIDByGAI
gai: str
Returns the numeric asset ID for the specified GAI.
GetAll
ids: list[intstr], subtype: str
Retrieves all data for a list of assets identified by their IDs and a subtype.
SQLQuery
query: str
Executes any SQL query and returns the results as a list of tuples.
MakeSource
id: int
Creates a source string for heap operations based on the function ID.
OpenFile
name: str, mode: str
Opens a file in the Eliona working directory (analogous to Python’s open()).
AddAssetTag
gai: str, tag: str
Adds a tag to an asset (if not already present).
RemoveAssetTag
gai: str, tag: str
Removes a tag from an asset (if present).
GetTrendRecords
asset_id: int, subtype: str, attr: str, begin: datetime
Retrieves historical measured values as a list of TrendRecord(ts, value) from the specified time onward.
WriteTrendRecords
asset_id: int, subtype: str, attr: str, `recs: TrendRecord
Writes one or more TrendRecord-records into the system.
GetAggregate
agg: Aggregate, asset_id: int, subtype: str, attr: str,raster: str, start: datetime,end: datetime = now()
Calculates aggregated values (Avg, Sum, Cusum) in a fixed raster over a period of time.
GetLastAggregate
agg: Aggregate, asset_id: int, subtype: str, attr: str, raster: str
Returns the most recent cumulative aggregate in the selected raster (incl. last_ts).
Example scripts
GetHeap
Description: Retrieves the current value of an attribute or the entire JSON of an asset from the heap.
Parameters:
gai: str(required) – GAI of the asset, e.g."K86_WP01"subtype: str(required) – data subtype, e.g."input"attribute: str(required) – attribute name, e.g."Aussentemperatur"
Code example:
Example output:
SetHeap
Description: Sends a dictionary of values to the Calculator to process them and store them in the heap.
Parameters:
gai: str(required)subtype: str(required)data: dict(required) – e.g.{"Aussentemperatur": 23.5}source: str(required) – typicallyeliona.MakeSource(id)
Code example:
GetAssetIDByGAI
Description: Returns the numeric asset ID for the specified GAI.
Parameters:
gai: str(required)
Code example:
GetAll
Description: Retrieves all data points of a specific subtype for a list of assets and returns them as a dictionary (GAI → data).
Parameters:
ids: list[int|str](required) – e.g.["K86_WP01", 1073]subtype: str(required)
Code example:
SQLQuery
Description: Executes any SQL query and returns the results as a list of tuples.
Parameters:
query: str(required)
Code example:
Example output:
MakeSource
Description:
Creates a source string (e.g. "ssr:123") for heap operations based on the function ID.
Parameters:
id: int(required)
Code example:
OpenFile
Description:
Opens a file in the Eliona working directory (analogous to Python’s open()).
Parameters:
name: str(required) – file name, e.g."log.txt"mode: str(required) – e.g."w","r"
Code example:
AddAssetTag
Description: Adds a tag to an asset if it has not already been assigned.
Parameters:
gai: str(required)tag: str(required)
Code example:
RemoveAssetTag
Description: Removes a tag from an asset, if present.
Parameters:
gai: str(required)tag: str(required)
Code example:
GetTrendRecords
Description:
Loads a list of historical measured values (TrendRecord(ts, value)) from the specified start time.
Parameters:
asset_id: int(required)subtype: str(required)attr: str(required)begin: datetime(required)
Code example:
WriteTrendRecords
Description:
Writes one or more TrendRecord-records into the system.
Parameters:
asset_id: int(required)subtype: str(required)attr: str(required)recs: TrendRecord | list[TrendRecord](required)
Code example:
GetAggregate
Description: Calculates aggregated values (average, sum, cumulative sum) in a fixed raster over a period of time.
Parameters:
agg: Aggregate(required) – e.g.eliona_types.Aggregate.Avgasset_id: int(required)subtype: str(required)attr: str(required)raster: str(required) – ISO 8601 duration, e.g."PT1H"for hourlystart: datetime(required)end: datetime(optional, default:now())
Code example:
Example output structure:
GetLastAggregate
Description:
Returns the most recent cumulative aggregate in the selected raster (including last_ts).
Parameters:
agg: Aggregate(required)asset_id: int(required)subtype: str(required)attr: str(required)raster: str(required)
Code example:
Example output structure:
Simple script for manipulating data
This example shows how a script generates a random value and writes it to the heap of an asset:
Description:
The script imports the module
random.A dictionary
datais created, containing a keypower_valto which a random value between 10 and 100 is assigned.The generated value is written to the asset heap with the method
SetHeapin the heap of the assetTestInactInwritten.
Retrieve and modify data
In this example, the value of an attribute is retrieved from one asset, modified, and stored in another asset:
Description:
The script imports the module
datetime.The current value of the attribute
power_valis retrieved from the heap of the assetTestInactInis retrieved.This value is tripled and written as
energy_valto the same asset.
Advanced functions and SQL queries
With the method SQLQuery any SQL queries can be executed directly from the script:
Description:
The SQL query retrieves all information about the asset with the GAI
TestInactIn.The result can then be processed further.
Was this helpful?