# Shelly switch turned on or off notifications on your phone

> Get a low-priority push notification when a Shelly relay or plug switches on or off.

- Company: Shelly (https://www.justpush.io/recipes/shelly)
- Event: `switch.on` (Switch on)
- Tags: Smart home, Monitoring
- Install: https://studio.justpush.io/recipes/shelly/switch-changed
- Web page: https://www.justpush.io/recipes/shelly/switch-changed

## Setup

1. Click Install in Studio and sign in. The recipe is added to your account and you get a personal webhook URL.
2. Open your Shelly device's web interface (browse to its IP address) or the Shelly Smart Control app and go to Actions.
3. Add a webhook action for the Switch on event.
4. Use the URL from the setup notes below. Shelly fills in the ${…} parts itself when it calls it.
5. Save, then trigger the device once to send a real event.
6. Use this as the action URL:
7. ```
8. your webhook URL (shown in Studio after install)?event=switch.on&device=${config.sys.device.name}&label=Garage%20heater
9. ```
10. Want a push when it turns off as well? Add a second action for `switch.off` with the same URL, changing `event=switch.on` to `event=switch.off`.
11. Change `label=Garage%20heater` to whatever you want the notification to say (`%20` is a space). Leave it out to use the device name.

## Code

Studio calls `handleRequest(request)` with the incoming webhook (`{ method, headers, body, query, raw }`) and sends the message object it returns. Returning `null` sends nothing.

```js
// Shelly → switch turned on / off
// Fires when a Shelly relay or plug (Plus 1, Plus Plug S, Pro 4PM, …) switches.
//
// Add webhook actions for "switch.on" and/or "switch.off", calling:
//   <integration URL>?event=switch.on&device=${config.sys.device.name}&label=Garage%20heater
// (use event=switch.off on the off action)

// Shelly webhooks are GET requests, so the data arrives as query parameters.
// Fall back to a JSON body so the recipe also works when POSTed (and in the Test runner).
function readParams(request) {
    const body = request.body && typeof request.body === "object" ? request.body : {}
    return { ...body, ...(request.query || {}) }
}

// Shelly fills an unset name in as "null"; treat that (and blanks) as missing.
function text(value) {
    const s = value == null ? "" : String(value).trim()
    return s === "" || s === "null" || s === "undefined" ? null : s
}

// Switch events, and how to describe them in the notification.
const STATES = {
    "switch.on": "on",
    "switch.off": "off",
}

function handleRequest(request) {
    const params = readParams(request)

    // Guard: only handle switch events, so other Shelly hooks on this integration stay quiet.
    if (!Object.prototype.hasOwnProperty.call(STATES, params.event)) {
        console.log(`Ignoring Shelly event "${params.event ?? "unknown"}" — not a switch event`)
        return null
    }

    // "label" is free text you put in the URL (e.g. "Garage heater"); fall back to the device name.
    const name = text(params.label) ?? text(params.device) ?? "Your Shelly"

    return {
        title: `🔌 ${name} switched ${STATES[params.event]}`,
        topic: "Shelly",
        priority: -1, // Low — a status update, not an alert
    }
}
```

## Shelly switch.on webhook payload (sample)

```json
{
  "event": "switch.on",
  "label": "Garage heater",
  "device": "shellyplusplugs-garage"
}
```

## FAQ

### Does this work on iPhone and Android?

Yes. Install the JustPush app from the App Store or Google Play and sign in. Every phone signed in to your account gets the notification.

### Do I need to write code?

No. Install the recipe in Studio, paste your webhook URL into Shelly and you are done. The code is there if you want to change the text, the sound or the buttons.

### Can I change what the notification says?

Yes. After install the recipe's code is yours. Edit it in Studio and test it against the sample payload before you save.

### What does it cost?

JustPush is free for 30 days. After that it's $19.99 a year, or $39.99 once. There is no extra charge for recipes.
