---
name: "saveDeviceLinkageScene"
mode: "api"
versionRequirements:
  - { name: "@tuya-miniapp/cloud-api", version: "1.1.0" }
---

## saveDeviceLinkageScene

> [VERSION] @tuya-miniapp/cloud-api >= 1.1.0

> 💡 This API depends on cloud capabilities; authorization must be configured in [Mini App Developer Platform](https://platform.tuya.com/miniapp/) under Development Settings - Cloud Capabilities.
> Note: This method supports only device DP types for conditions and actions, and devices must be in the current home. Starting from @tuya-miniapp/cloud-api 1.4.0, scenes saved via this method appear in the app's Scenes tab and can be queried via getSceneAndAuto, getSceneListByHomeID, etc.

### Description

Save or edit a scene (device-to-device linkage only)

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `params` | `SaveDeviceLinkageSceneParams` | Yes | Request body |

### Return Value

Type: `Promise<SaveDeviceLinkageSceneResult>`

SaveDeviceLinkageSceneResult (see type definitions and minituya return parameters for field meanings)

##### SaveDeviceLinkageSceneResult

| Property | Type | Description |
| --- | --- | --- |
| `name` | `string` | Scene name |
| `ruleId` | `string` | Scene ID |
| `ruleGenre` | `number` | Rule type: 1 - Scene, 2 - Automation |

### Referenced Types

##### `interface` SaveDeviceLinkageSceneParams

| Property | Type | Description |
| --- | --- | --- |
| `gid` | `string` | Home ID |
| `name` | `string` | Scene name |
| `coverIcon` | `string` | Icon |
| `actions` | `SceneActionInput[]` | Action list |
| `conditions` | `SceneCondition[]` | Condition list: required when creating/updating an automation; not required when creating/updating a one-tap execution |
| `enabled` | `boolean` | Whether enabled |
| `matchType` | `number` | Match type: 1 - execute when any condition is met; 2 - execute when all conditions are met |
| `displayColor` | `string` | Background color |
| `id` | `string` | Scene ID: required when updating, not required when creating |

##### `type` SceneActionInput

| Property | Type | Description |
| --- | --- | --- |
| `id` | `string` | Action ID: not required when creating, required when updating |
| `devDelMark` | `boolean` | Whether the associated device has been removed |
| `entityId` | `string` | Device ID |
| `executorProperty` | `Record<string, string \| number \| false \| true>` | Execution properties |
| `extraProperty` | `Record<string, unknown>` | Additional properties |

##### `type` SceneCondition

| Property | Type | Description |
| --- | --- | --- |
| `id` | `string` | Condition ID: not required when creating, required when updating |
| `entitySubIds` | `string` | dpId associated with the condition |
| `iconUrl` | `string` | Product icon URL |
| `entityId` | `string` | Device ID associated with the condition |
| `expr` | `string \| number[][]` | Condition expression |
| `devDelMark` | `boolean` | Whether the associated device has been removed |
| `entityType` | `number` | Condition type: 1 - less than/equal to/greater than; 41 - within range/out of range |
| `extraInfo` | `Record<string, unknown>` | Additional information |


### Examples

#### Request example

```typescript
import { saveDeviceLinkageScene } from '@tuya-miniapp/cloud-api';

// Create scene
saveDeviceLinkageScene({
  gid: '123456789',
  name: 'Home Mode',
  coverIcon: 'https://example.com/icon.png',
  actions: [
    {
      devDelMark: false,
      entityId: 'device_001',
      executorProperty: {
        '1': true,
        '2': 50,
      },
      extraProperty: {},
    },
  ],
  conditions: [
    {
      entitySubIds: '1',
      iconUrl: 'https://example.com/icon.png',
      entityId: 'device_002',
      expr: [['1', '==', 'true']],
      devDelMark: false,
      entityType: 1,
      extraInfo: {},
    },
  ],
  enabled: true,
  matchType: 1,
  displayColor: '#FF5722',
})
  .then(response => {
    console.log('Scene saved:', response);
  })
  .catch(error => {
    console.error('Failed to save scene:', error);
  });

// Update scene
saveDeviceLinkageScene({
  gid: '123456789',
  id: 'scene_001',
  name: 'Home Mode (Updated)',
  coverIcon: 'https://example.com/icon.png',
  actions: [
    {
      id: 'action_001',
      devDelMark: false,
      entityId: 'device_001',
      executorProperty: {
        '1': true,
        '2': 80,
      },
      extraProperty: {},
    },
  ],
  conditions: [],
  enabled: true,
  matchType: 1,
  displayColor: '#FF5722',
})
  .then(response => {
    console.log('Scene updated:', response);
  })
  .catch(error => {
    console.error('Failed to update scene:', error);
  });
```

#### Response example

```json
{
  "name": "Home mode",
  "ruleId": "scene_001",
  "ruleGenre": 1
}
```
