---
name: "bindRule"
mode: "api"
versionRequirements:
  - { name: "@ray-js/ray", version: "1.7.60" }
title: "bindRule - 将按键条件与场景规则关联"
---

## bindRule

> [VERSION] @ray-js/ray >= 1.7.60

### 描述

将按键条件与场景规则关联，建立设备按键与联动规则的绑定关系

### 参数

`Params`

| 参数 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| `params` | `IBindRule` | 是 | 绑定规则参数 |

### 返回值

类型: `Promise<IBindRuleResponse>`

绑定结果信息

##### IBindRuleResponse

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `associativeEntityId` | `AssociativeEntityId` | 与面板/设备建立绑定关系,通常为设备ID、DP点、群组、规则，dpId#value等 |
| `associativeEntityValue` | `AssociativeEntityValue` | 当associativeEntityId不足以区分情况下使用，通常可关联设备、DP值、群组、规则等 |
| `bizDomain` | `string` | 业务域 |
| `id` | `number` | 数据 ID |
| `ownerId` | `string` | 家庭 ID |
| `sourceEntityId` | `string` | 设备 ID |
| `triggerRuleId` | `string` | 规则 ID |
| `triggerRuleVO` | `any` | 执行动作设备信息 |

### 引用对象

##### `interface` IBindRule

| 属性 | 类型 | 描述 |
| --- | --- | --- |
| `devId` | `string` | 设备 ID |
| `sourceEntityId` | `string` | 与面板/设备建立绑定关系,通常为设备ID、DP点、群组、规则，dpId#value等 |
| `associativeEntityId` | `AssociativeEntityId` | 当associativeEntityId不足以区分情况下使用，通常可关联设备、DP值、群组、规则等 |
| `ruleId` | `string` | 关联场景 ID |
| `entitySubIds` | `string` | 关联 DP 点 |
| `expr` | `[string[]]` | 关联 DP 点、动作组合 |
| `bizDomain` | `string` | 业务域 |
| `gid` | `string` | 家庭 ID |
| `property` | `Record<string, any>` | 拓展字段，非必传 |

##### `type` AssociativeEntityId

与面板/设备建立绑定关系,通常为设备ID、DP点、群组、规则，dpId#value等

```typescript
type AssociativeEntityId = string;
```

##### `type` AssociativeEntityValue

当associativeEntityId不足以区分情况下使用，通常可关联设备、DP值、群组、规则等

```typescript
type AssociativeEntityValue = string;
```


### 示例代码

```typescript
import { bindRule } from '@ray-js/ray';

const bindSceneRule = async () => {
  try {
    const result = await bindRule({
      devId: 'device_001',
      associativeEntityId: '1',
      ruleId: 'rule_001',
      entitySubIds: '1',
      expr: [['$dp1', '==', 'true']],
      bizDomain: 'wirelessSwitch',
      gid: '12345678',
    });
    console.log('绑定成功, ID:', result.id);
  } catch (err) {
    console.error('绑定失败:', err);
  }
};
```
