Skip to main content
Version: 1.0

Tutorial: Smart Light Automation

Build a practical automation script that responds to buttons and motion sensors.

What You'll Build

A script that:

  • Toggles your light when you press a button
  • Shows light status on double press
  • Turns on the light when motion is detected
  • Automatically turns off after a timeout

Prerequisites

  • Shelly Gen2+ device with scripting support
  • Access to the device's web interface
  • Optional: Shelly BLU Motion sensor for motion detection
Device Compatibility

This tutorial uses the CCT component from a Shelly Pro RGBWW PM. If you have a different device, replace CCT with your device's output component:

  • Relay devices (Plus 1PM, Pro 2PM, etc.): use Switch
  • Dimmer devices (Wall Dimmer): use Light
  • RGBW devices (Plus RGBW PM): use RGBW

Check your device's documentation for available components.

Step 1: Create a Script

Open your device's web interface and navigate to Scripts in the sidebar.

Scripts menu

Click Create script to create a new script.

Create script

Enter a name like smart_light and click Create. You'll see an empty code editor.

Step 2: Hello World

Let's start with a simple script. Paste this code into the editor:

// My first Shelly script
console.log("Smart Light automation starting...");

let info = Shelly.getDeviceInfo();
console.log("Running on:", info.app);
console.log("Firmware:", info.ver);

Click Save, then click Start to run the script.

Hello World

You should see output in the Console area below the editor showing your device name and firmware version.

Step 3: Handle Button Presses

Now let's make the script respond to button presses. Replace your code with:

let CONFIG = {
lightId: 0,
buttonInput: "input:0"
};

function handleEvent(event) {
if (event.component === CONFIG.buttonInput) {
if (event.info.event === "single_push") {
console.log("Button pressed - toggling light");
Shelly.call("CCT.Toggle", {id: CONFIG.lightId});
}
else if (event.info.event === "double_push") {
let status = Shelly.getComponentStatus("cct", CONFIG.lightId);
console.log("Light is", status.output ? "ON" : "OFF");
console.log("Brightness:", status.brightness, "%");
}
}
}

Shelly.addEventHandler(handleEvent);
console.log("Button handler ready!");

Click Save, then Start. Now when you press the physical button:

  • Single press: toggles the light on/off
  • Double press: shows current light status in the console

Step 4: Add Motion Detection

No motion sensor?

If you don't have a Shelly BLU Motion sensor, you can skip this step. The button control from Step 3 will still work. You can add motion detection later when you have a sensor configured.

Let's add automatic light control based on motion detection. This uses a BTHome motion sensor (like Shelly BLU Motion) that's been added to your device.

Replace your code with:

let CONFIG = {
lightId: 0,
buttonInput: "input:0",
motionSensorId: 200, // BTHomeSensor component ID
motionTimeout: 300000 // 5 minutes in milliseconds
};

let motionTimer = null;

function handleEvent(event) {
if (event.component === CONFIG.buttonInput) {
if (event.info.event === "single_push") {
console.log("Button - toggling light");
Shelly.call("CCT.Toggle", {id: CONFIG.lightId});
}
else if (event.info.event === "double_push") {
let status = Shelly.getComponentStatus("cct", CONFIG.lightId);
console.log("Light:", status.output ? "ON" : "OFF",
"at", status.brightness + "%");
}
}
}

function handleStatus(status) {
// Check for motion sensor updates
if (status.component === "bthomesensor:" + CONFIG.motionSensorId) {
if (status.delta.value === true) {
console.log("Motion detected!");
Shelly.call("CCT.Set", {id: CONFIG.lightId, on: true});

// Reset the auto-off timer
Timer.clear(motionTimer);
motionTimer = Timer.set(CONFIG.motionTimeout, false, function() {
console.log("No motion - turning off");
Shelly.call("CCT.Set", {id: CONFIG.lightId, on: false});
});
}
}
}

Shelly.addEventHandler(handleEvent);
Shelly.addStatusHandler(handleStatus);

console.log("Smart Light ready!");
console.log("- Single press: toggle light");
console.log("- Double press: show status");
console.log("- Motion sensor: auto-on with timeout");

Motion code

The script now:

  1. Listens for button events (single and double press)
  2. Listens for motion sensor status updates
  3. Turns on the light when motion is detected
  4. Starts a 5-minute timer that turns off the light if no more motion is detected

Step 5: Enable Auto-Start

To run your script automatically when the device boots, go back to the Scripts list and enable Run on startup for your script.

Auto-start

Complete Script

Here's the final script with error handling:

/**
* Smart Light Automation
* Controls light via button and motion sensor
*/

let CONFIG = {
lightId: 0,
buttonInput: "input:0",
motionSensorId: 200,
motionTimeout: 300000
};

let motionTimer = null;

function handleEvent(event) {
try {
if (event.component === CONFIG.buttonInput) {
if (event.info.event === "single_push") {
Shelly.call("CCT.Toggle", {id: CONFIG.lightId});
console.log("Light toggled");
}
else if (event.info.event === "double_push") {
let status = Shelly.getComponentStatus("cct", CONFIG.lightId);
if (status) {
console.log("Status:", status.output ? "ON" : "OFF",
"Brightness:", status.brightness + "%");
}
}
}
} catch (e) {
console.log("Error:", e.message);
}
}

function handleStatus(status) {
if (status.component === "bthomesensor:" + CONFIG.motionSensorId) {
if (status.delta.value === true) {
console.log("Motion - light on");
Shelly.call("CCT.Set", {id: CONFIG.lightId, on: true});

Timer.clear(motionTimer);
motionTimer = Timer.set(CONFIG.motionTimeout, false, function() {
console.log("Timeout - light off");
Shelly.call("CCT.Set", {id: CONFIG.lightId, on: false});
});
}
}
}

Shelly.addEventHandler(handleEvent);
Shelly.addStatusHandler(handleStatus);

console.log("Smart Light v1.0 ready");

Next Steps