SFA Templates

SCADA Final Aggregator uses jinja2 template engine to serve server-side templates. You can use SFA templates for regular HTML, javascript and JSON data files. Both ui and pvt folders can contain template files, the difference is only that templates in ui are public while templates in pvt are served via SFA PVT.

Template files

All files with .j2 extension are processed as templates, index.j2 has more priority than index.html as the primary interface page.

Templates support all jinja2 functions and features, plus have some specific built-in variables and functions.

Template variables

The following variables are available in all templates:

  • All custom user-defined variables
  • server contains a dict with a system and current API key info (equal to SFA API test function result) plus an additional key remote_ip which contains either request IP address or value of X-Real-IP variable (if set by frontend server).
  • request contains CherryPy request object, e.g. display user agent:
{{ request.headers.get('User-Agent') }}

Template functions

All templates have the following built-in functions. Template functions never raise exceptions, instead they return None values.


Get list of item groups

groups(g=None, p=None, k=None)


The function is similar to SFA API groups except that if API key is not specified, the current key is used.


Get list of items and their state

state(i=None, g=None, p=None, k=None):


  • i full item id (group/id), optional
  • g filter by group (use MQTT-style wildcards)
  • p item type (U for unit, S for sensor, LV for lvar), required if ID is not in oid format
  • k API key (use key ID instead of key itself)

The function is similar to SFA API state except that if API key is not specified, the current key is used.


Allows to call any SFA API method directly.

api_call(method, params={}, k=None)


  • method API method to call
  • params API call parameters
  • k API key (use key ID instead of key itself)

Example. Let’s warn user when specified UC controller is not connected:

{%- set controller = api_call(
            'get_controller', { 'i': 'uc/mws1-v1' }, 'masterkey') %}
{%- if not controller.connected %}
    UC controller is not connected
{%- endif %}


Get current API call info. Valid params are:

  • id unique API request ID
  • u current user
  • utp current user type
  • key_id current API key id


Logged in as: {{ get_aci('u') }}


Import any available Python module (mapped to importlib.import_module)


{% set os=import_module('os') %}
Server PID: {{ os.getpid() }}