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

## bindRule

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

### Description

Associate button conditions with scene rules to bind device buttons to linkage rules

### Parameters

`Params`

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| `params` | `IBindRule` | Yes | Parameters for binding rules |

### Return Value

Type: `Promise<IBindRuleResponse>`

Binding result information

##### IBindRuleResponse

| Property | Type | Description |
| --- | --- | --- |
| `associativeEntityId` | `AssociativeEntityId` | Establish a binding with the panel/device, typically using device ID, DP point, group, rule, dpId#value, etc. |
| `associativeEntityValue` | `AssociativeEntityValue` | Used when associativeEntityId is insufficient to distinguish; can typically associate device, DP value, group, rule, etc. |
| `bizDomain` | `string` | Business domain |
| `id` | `number` | Data ID |
| `ownerId` | `string` | Home ID |
| `sourceEntityId` | `string` | Device ID |
| `triggerRuleId` | `string` | Rule ID |
| `triggerRuleVO` | `any` | Device information for execution actions |

### Referenced Types

##### `interface` IBindRule

| Property | Type | Description |
| --- | --- | --- |
| `devId` | `string` | Device ID |
| `sourceEntityId` | `string` | Establish a binding relationship with the panel/device; typically a device ID, DP point, group, rule, dpId#value, etc. |
| `associativeEntityId` | `AssociativeEntityId` | Use when associativeEntityId is insufficient to distinguish; typically can associate devices, DP values, groups, rules, etc. |
| `ruleId` | `string` | Associated scene ID |
| `entitySubIds` | `string` | Associated DP point |
| `expr` | `[string[]]` | Associated DP point and action combination |
| `bizDomain` | `string` | Business domain |
| `gid` | `string` | Home ID |
| `property` | `Record<string, any>` | Extension field, optional |

##### `type` AssociativeEntityId

Binding with panel/device, typically device ID, DP point, group, rule, dpId#value, etc.

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

##### `type` AssociativeEntityValue

Use when associativeEntityId is insufficient to distinguish; typically can associate devices, DP values, groups, rules, etc.

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


### Examples

```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('Bind succeeded, ID:', result.id);
  } catch (err) {
    console.error('Bind failed:', err);
  }
};
```
