跳到主要内容

Automation Store

AutomationStore is the automation rules engine. It stores device automations triggered by node changes, weather conditions, or time events.

What it does

Automations let you create "if this, then that" rules:

  • If a sensor value reaches a threshold → Turn on a fan
  • If the sun sets → Turn on lights
  • If a parameter changes → Trigger an action on another device

The store manages all automations for all groups. Create, update, and delete via group entity methods.

API reference

Properties

PropertyTypePurpose
automationsListESPCDFAutomation[]All automations (computed from internal maps)

Getting the store

import { initCDF } from "@espressif/rainmaker-base-cdf";

const espCDF = await initCDF({ sdkAdaptorRegistry });
const { automationStore } = espCDF;

How to use it

Load automations

const group = espCDF.groupStore.getGroupById("group-id")!;
const { data: automations } = await group.getAutomations();

// automationStore is now populated
console.log(espCDF.automationStore.automationsList.length);

Query automations

// All automations
const automations = espCDF.automationStore.automationsList;

// Single automation by ID
const auto = espCDF.automationStore.getAutomationById("auto-id");

// Filter by type in UI code (node, weather, daylight)
const nodeAutomations = automations.filter(a => a.eventType === "node");
const daylightAutomations = automations.filter(a => a.eventType === "daylight");

Create a node-triggered automation

const group = espCDF.groupStore.getGroupById("group-id")!;

const nodeAuto = await group.createAutomation({
name: "Turn off fan when temperature drops",
enabled: true,
eventType: "node",
nodeId: "sensor-node-id",
events: [{ check: { name: "Temperature", operator: "<=", value: 22 } }],
eventOperator: "any",
actions: [{ nodeId: "fan-node-id", action: { Fan: { Power: false } } }],
});

Create a daylight-triggered automation

const daylightAuto = await group.createAutomation({
name: "Lights on at sunset",
enabled: true,
eventType: "daylight",
events: [{ check: { name: "sunset" } }],
actions: [{ nodeId: "light-node-id", action: { Light: { Power: true } } }],
location: { latitude: 18.5, longitude: 73.9 },
});

GroupStoreSynchronizer adds the returned entity to AutomationStore automatically.

Update and delete automations

const auto = espCDF.automationStore.getAutomationById("auto-id")!;

await auto.update({ enabled: true });
await auto.update({ name: "New name", enabled: true, events: [...], actions: [...] });

// Delete — AutomationStoreSynchronizer removes it from the store
await auto.delete();

Handle pagination

For large automation lists, use pagination per adaptor:

const adaptorId = espCDF.getActiveAdaptorIdentifier()!;
if (espCDF.automationStore.hasNext(adaptorId)) {
await espCDF.automationStore.fetchNext(adaptorId);
}

Reactive automation list

import { observer } from "mobx-react-lite";
import type { ESPCDF } from "@espressif/rainmaker-base-cdf";

const AutomationList = observer(function AutomationList({
espCDF,
}: {
espCDF: ESPCDF;
}) {
const automations = espCDF.automationStore.automationsList;
return (
<FlatList
data={automations}
keyExtractor={(a) => a.id}
renderItem={({ item }) => (
<Text style={{ opacity: item.enabled ? 1 : 0.4 }}>{item.name}</Text>
)}
/>
);
});

See also

Implementation

API Reference

On this page