documentation

API Documentation

Create Scene

The scene represents some action that the user sets. Scenes are used for home automation. We can create a scene in two ways:

Creating

Script Scene

Open script scene modal

First we add a name for the scene. We can create out own name or use a template.

Add script scene modal

Examples:

and many others

Every template has a short description about the processes it executes.

Scene parts:

Script scene parts
Part Description
Rooms Organize your devices by phisical space
Devices All your added Shelly products
Parameters Input values used to run with chosen settings (depends on template)

Scenes are allowed to be executed in specific intervals:

Time interval

⚠️ CAUTION At least 1 day must be selected. Time interval must be at least 1 minute long.

If all of the previos steps are completed, script is generated.

const params = {
  // add parameters here
};

function init() {
  // initialization
}

function when() {
  // check if parameters meet a condition
}

function doBlock() {
  // trigger action when a condition is met
}

Functions

init() - Initialization of the scene. Happens once.

when() - Defines the condition under which the automation should run.

doBlock() - Triggers the action if when() is true.

Manual Scene

Open scene modal

We start with adding a condition (rule that must be true to run an action)

There are several conditions between which the user can choose:

Scene add condition
Condition Description
Device based Scene runs only when a specific Shelly device state/value matches a rule
Time based Scene runs only when the current time/date matches rules
Alarm based Scene runs with an alarm set
Weather forecast based Scene can trigger based on the forecasted weather for your location (not a local sensor).
Sunrise/Sunset based Scene that triggers actions according to the sunrise or sunset time for your location (automatically adapts by season)

We can add conditions and combine them with AND/OR.

Scene conditions

Then we add actions:

Scene actions
Action Description
Device What the app should do to a device when the scene triggers
Notify Sends a notification when the scene triggers
Group When a scene triggers - turn on/off a group of devices
Scene Execute actions, enable or disable scene
Alarm Enables or disables an alarm

Scenes are allowed to be executed in specific intervals:

Time interval

⚠️ CAUTION At least 1 day must be selected. Time interval must be at least 1 minute long.

More options:

Select a room (“folder” that groups devices by their physical location)

Details:

Scene details

Script Structure

What is a script

The script represents a JavaScript program that runs on the Shelly device (or in your Shelly environment) to create custom automation.

With scripts you can do things like:

The program contains 3 functions:

Functions

init()

This is the script’s startup function. When the script is started, init() is called first to:

when()

Returns true when the parameters meet a given condition/event.

doBlock()

Runs if the when() block returned true. doBlock() triggers a defined action when the condition(s) is met.

Parameters and variables

You can have params block. This is where the scene/script stores user-editable settings in one place.

const params = {
  maxTemp: 25,
  minTemp: 22,
};

The script then reads these values in when() and doBlock().

Variables are used to store and reuse values.

Why they matter:

Creating a Script

You can create a script in 3 different ways. For the first 2 ways - “Create with your own name” and “Use a template” you press the JS button is the navigation menu.

Open script scene modal
  1. Create with your own name

In the modal you enter a name for the script.

Create your own script modal

You start a blank script where you write/paste your code. Best for custom logic and advanced automations.

Create your own script
  1. Use a template

In the modal you pick a ready-made example.

Create a script from a template

After you picked a template, you need to choose a room and devices to use.

Description for the script template

Then you choose a time interval where the script is active

Time interval for script template

Then your example is ready and you can adjust the values or keep it as it is. This is the fastest way to get a working baseline.

  1. Convert a Scene to a script

The app generates a script from an existing scene, so you can extend it with more complex logic (variables, loops, extra conditions).

First, you select the scene you want to convert. Second, on the right menu you press this button <>. A button for converting appears.

Convert a scene to a script

When you press it an alert for confirmation is displayed at the top of the screen.

Confirm scene conversion

Script Templates

We use templates to create a script because they give a working starting point

Main benefits:

When you press the JS button in the All Scenes screen a modal pops up.

Open templates modal Templates modal

Templates

Air condition heating - turns AC on/off based on room temperature.

Auto power saver - turns off house lights and AC when the main door is closed.

Smart curtain controller - opens or closes the curtains based on light levels.

Humidy regulator - turns the humidifier on when the air is too dry and off when humidity rises above the ideal range.

Motion light - turns the light on when motion is detected.

Mourning routine starter - turns on the kitchen light and coffee machine at 7:00 AM on weekdays.

Night power saver - turns off the TV and AC during late-night hours when no motion is detected.

Window alert - sends a phone notification whenever the window is opened.

Smart AC controller - turns on the AC when it’s hot, but only if the window is closed, and turns if off when the temperature drops.

Auto power saver with motion - turns TV, lights and AC off when there is no motion in living room and bedroom for 15 minutes. If motion is detected again after 18:00, the lights turn on.

Window aware heating - turns heating on when temperature is below threshold and all windows are closed.

Weekday morning starter - turns on the kitchen light and coffee machine at 7:00 AM on weekdays. If it’s dark and someone is at home - both turn on; if it is bright - only the coffee machine turns on.

Night motion control - turns off lights and AC between 00:00 and 05:00 if there is no motion. If motion is detected during this period, only the hallway light turns on, while AC stays off.

Smart bathroom heating - controls bathroom heating based on temperature and motion. If temperature falls below 20°C and motion is detected - heating turns on.

Smart entry control - controls hallway light and TV based on door opening time and room motion.

Smart hallway lighting - turns on hallway light when motion is detected and it is dark. The light stays on for 5 minutes, then turns off if no further motion is detected.

Smart pool heating - controls pool heating based on water temperature and pump status.

Garage motion light - controls garage light based on two motion sensors. If either motion sensor detects movement - light turns on.

Button door control - controls doors based on button presses. If any button is pressed twice - front door closes. If any button is pressed three times - garage door opens.

Car charge status - sends notifications based on car charging status using power meter.

AC window control - controls AC based on window status and temperature. If AC is on and the window opens - AC turns off and a notification is send.

Morning temp control - controls kitchen heating based on morning temperature.

Motion aware living room - turns on living room light based on motion and light.

Evening heating control - controls evening heating based on temperature and window status.

Night auto-off and coridor light - turns off TV and AC if there is no motion at night for at least 15 minutes. Turns on corridor light if motion is detected.

Script Page Buttons

On this page you have 2 buttons:

You can add one of the following entries:

Device

Devices are added as variables and can be used anywhere in the script

Nested scene

Nested scenes are added as variables and can be used anywhere in the script

Alarm

Alarms are added as variables and can be used anywhere in the script

Group

Groups are added as variables and can be used anywhere in the script

Condition

Device types

Device Properties
button battery, pressedLong, pressedOnce, pressedThreeTimes, pressedTwice
emeter add-on temperature, consumed,consumption, energy consumption, power consumed, power measured, voltage, turn on/off, toggle,
light brightness, choosing color, choosing temperature, color, consumed, consumption, dim, double pressed, double pushed, energy, mode, on/off, power consumed, power consumption, temperature, toggle, add-on temperature, external sensor temperature, add-on humidity
relay power, connected, off, on, , pressedLong, pressed once, pressed three times, pressed twice, temperature, add-on humidity, connected, consumed, consumption, toggle, illumination
sensor add-on temperature, battery, battery health, close, closed, connected, gas concentration, gas detected, humidity, illumination, motion detected, on/off, temperature, tilt, vibration detected, voltage value,

Then a comparator (comparison operator) is used to check whether the condition meets a specified value (in %).

The property is watched as a trigger or a condition

Property as a Trigger

Trigger is the event that starts an automation running. You can choose one of the following trigger cases:

The trigger is initialized in the init() function. when() checks whether the condition is met and returns true/false

Property as a Condition

Checks a specific device parameter and triggers an action only if it matches a rule

Action

Has 3 condition types

Action type Contains
Device You choose a device and actions to apply to it (on, off)
Notification Could be activity log or alexa notification
Scene You choose a scene and apply scene actions - disable, enable, isFulfilled, runDo, toggle, trigger

Actions are added to the doBlock() function.

Device actions set values to parameters:

fridge.on = {
  value: true,
  timeout: { wait: 45, unit: "seconds" },
  delay: { wait: 5, unit: "seconds" },
};

Only relays have timeout

While scene actions and notification actions call functions with 1 or more parameters passed to the function:

Scene action:

myTestScene.runDo();

Notification action:

sceneUtils.logActivity(message);

Active time

You set an interval in which an action can be performed. First select a day(s) of the week using a checkbox.

⚠️ At least 1 day must be selected.

Then a start and end time interval is selected.

⚠️ The interval must be at least 1 minute

Active time is initialized in the init() function

Global variable

Global variables are persistent values that the script can save and reuse (even after reboot). These variables can have 1 of the 2 types:

Type Description
Get Reads the saved value (loads it into your script logic)
Set Writes/Updates the value

The variable has a name and is added to a block.

If the type is Get it can be used in:

If the type is Set you add a value to the variable. If no value is entered, the variable is undefined

Variables with type Set can be used in:

When a variable is first defined, by default it is initialized in the init block

Solar condition

This is a sun event-based trigger using your location.

Fields

This event is added to the init block with its parameters

Scene examples

Here are some examples of scenes created using templates. Templates are easy to use and they give a working starting point.

First example

Automatically turns on the kitchen light and coffee machine at 7:00 AM on weekdays.

// Generated Parameters
const params = {};

var kitchenLight = deviceFactory.getDevice("use_id_here"); // Light
var coffeeMachine = deviceFactory.getDevice("use_id_here"); // Coffee

function init() {
  sceneUtils.setActiveTime({
    start: "00:00",
    duration: "1439",
    days: [1, 2, 3, 4, 5, 6, 7],
  });
}

function when() {
  return sceneUtils.checkSchedule("07:00", [1, 2, 3, 4, 5]);
}

function doBlock() {
  kitchenLight.on = true;
  coffeeMachine.on = true;
}

Summary

Every Monday–Friday at 07:00 AM, it turns Kitchen Light = ON and Coffee Machine = ON.

How it works

Second example

Automatically turns the light on when motion is detected in low light conditions and turns it off when no motion is detected.

// Generated Parameters
const params = {
  illuminanceValue: 50,
};

var light = deviceFactory.getDevice("use_id_here"); // Color Light
var motionSensor = deviceFactory.getDevice("use_id_here"); // Motion Sensor

function init() {
  motionSensor.setTriggerOnAnyChange("motion");
  motionSensor.setRepeatedTrigger("illuminance < " + params.illuminanceValue);
  sceneUtils.setActiveTime({
    start: "18:00",
    duration: "720",
    days: [1, 2, 3, 4, 5, 6, 7],
  });
}

function when() {
  return (
    motionSensor.motion && motionSensor.illuminance < params.illuminanceValue
  );
}

function doBlock() {
  if (light.off) {
    light.on = true;
  } else if (motionSensor.noMotion) {
    light.off = true;
  }
}

Summary

The script is active between 18:00 and 06:00 (720 min), every day. If motion is detected and illuminance < 50 → turns the light ON. When the sensor reports noMotion → turns the light OFF.

How it works

Third example

Turns off house lights and AC automatically when the main door is closed.

// Generated Parameters
const params = {};

var mainDoor = deviceFactory.getDevice("use_id_here"); // Stove
var houseLights = deviceFactory.getDevice("use_id_here"); // Color Light
var ac = deviceFactory.getDevice("use_id_here"); // Air conditioner

function init() {
  mainDoor.setTriggerOnce("closed");
  sceneUtils.setActiveTime({
    start: "00:00",
    duration: "1439",
    days: [1, 2, 3, 4, 5, 6, 7],
  });
}

function when() {
  return houseLights.on || ac.on;
}

function doBlock() {
  houseLights.off = true;
  ac.off = true;
}

Summary

A trigger is set when the door is closed. This turns OFF the house lights and the ac. The script is active from 00:00 to 23:59, Monday-Sunday

How it works

Fourth example

Automatically turns AC on/off based on room temperature.

// Generated Parameters
const params = {
  maxTemp: 25,
  minTemp: 22,
};

var ac = deviceFactory.getDevice("use_id_here"); // Air conditioner
var tempSensor = deviceFactory.getDevice("use_id_here"); //Livingroom Temperature

function init() {
  sceneUtils.setActiveTime({
    start: "00:00",
    duration: "1439",
    days: [1, 2, 3, 4, 5, 6, 7],
  });
  tempSensor.setRepeatedTrigger("temperature < " + params.minTemp);
  tempSensor.setRepeatedTrigger("temperature > " + params.maxTemp);
}

function when() {
  return (
    tempSensor.temperature < params.minTemp ||
    tempSensor.temperature > params.maxTemp
  );
}

function doBlock() {
  if (tempSensor.temperature < params.minTemp) {
    ac.on = true;
  } else if (tempSensor.temperature > params.maxTemp) {
    ac.on = false;
  }
}

Summary

Works from 00:00 to 23:59, all days.

How it works