Quick Automation Guide

Quick guide for scheduling jobs:

At the moment the automation section is not in the main menu yet. You can access it through the url tenant//automation

There are two tabs: templates and jobs. In order to schedule jobs, you need the jobs tab. In order to schedule a job, click on the “+ Schedule Job” button.

A screen will show up that will allow you to select devices, a date and type in some python code.

Devices can be selected per device, device tag. You can also schedule jobs without devices.

The schedule time determines when the job will be executed. Default is now. The tomorrow-button schedules the job for tomorrow, 17:00. You can also edit the date manually, as long as the date format remains the same.

Python scripts need to contain a function. This function will be executed when the job is run. When you don’t have a function, saving the job will result in an error. When you have multiple functions, the first one will be executed. When you want another one to be executed, you can use the @job_function decorator like this:

def first_function():
    print("This will not be executed")

@job_function
def second_function():
    print("This will be executed")

Anything that you print in your python function will be logged to the job, available in the job’s log tab.

When you call your function with the parameter devices, you can access the devices you selected:

def main(devices):
    for device in devices:
        print(f"Device: {device.name}, address: {device.ipaddress}")

When you try to use other parameters, the form will return an error.

In order to talk to devices, you can use the function device_cli of the app.utils.helpers library. At the moment this library only contains this function, but in the future it will be expanded with more different useful functions.

from app.utils.helpers import device_cli

async def main(devices):
    for device in devices:
        print(f"Device: {device.name}, IP: {device.ipaddress}")
        result = await device_cli("show clock", device)
        print(f"Result: '{result}'")

Do note that device_cli is an asynchronous function, so if you want to use it, be sure to define your function as asynchronous.

You can create scripts in advance using templates. The left tab in the automation section allows you to do this. When you then schedule a job, you can select the template you made and it will automatically appear in your editor. If you want you can then still make modifications.

You can pass variables to your script to be executed:

from app.utils.helpers import device_cli

async def main(var1):
    print(f"Variable 1: {var1}")

The value for this variable can then be provided when you create a job. These names should match the ones you have defined in your script. Variable names must be valid python variable names, and also “devices” and “api” are not allowed, since they clash with pre-defined variables. Variables can be empty. You can also use predefined variables:

from app.utils.helpers import device_cli

async def main(var1="value1"):
    print(f"Variable 1: {var1}")

Note that for this to work, you need to not send a variable called “var1” along when creating the job. If you send it empty, the scheduler will assume an empty string instead of the default value.