Automation Store
The AutomationStore manages automation triggers and automation management within your application. Automations allow you to create context-aware rules that trigger device actions based on various conditions like node parameters, weather, or daylight.
Overview
The AutomationStore provides:
- Automation list management and synchronization
- Automation organization by event type (node, weather, daylight)
- Geographic coordinates management
- Enable/disable automation functionality
- Automatic synchronization with backend
- Reactive state management
Accessing the Store
Access the AutomationStore through the CDF instance:
const automationStore = espCDF.automationStore;
Constructors
constructor
Creates a new instance of AutomationStore.
new AutomationStore(rootStore?: CDF)
Parameters:
rootStore(optional): The CDF root store instance.
Properties
| Property | Type | Description |
|---|---|---|
| automationList | ESPAutomation[] (computed) | A computed array of all automations in the store (combines node, weather, and daylight automations). |
| nodeAutomationList | ESPAutomation[] (computed) | A computed array of node-based automations (automations triggered by node parameter changes). |
| weatherAutomationList | ESPAutomation[] (computed) | A computed array of weather-based automations (automations triggered by weather conditions). |
| daylightAutomationList | ESPAutomation[] (computed) | A computed array of daylight-based automations (automations triggered by daylight/sunset conditions). |
| nodeAutomations | { [key: string]: ESPAutomation } | A dictionary of node-based automations indexed by automation ID. |
| weatherAutomations | { [key: string]: ESPAutomation } | A dictionary of weather-based automations indexed by automation ID. |
| daylightAutomations | { [key: string]: ESPAutomation } | A dictionary of daylight-based automations indexed by automation ID. |
| geoCoordinates | ESPGeoCoordinates | null | Geographic coordinates used for weather and daylight automations. |
| hasNext | boolean | Indicates if there are more automations to fetch (pagination). |
| enabledAutomations | ESPAutomation[] (computed) | All enabled automations. |
| disabledAutomations | ESPAutomation[] (computed) | All disabled automations. |
| enabledNodeAutomations | ESPAutomation[] (computed) | Enabled node automations. |
| disabledNodeAutomations | ESPAutomation[] (computed) | Disabled node automations. |
| enabledWeatherAutomations | ESPAutomation[] (computed) | Enabled weather automations. |
| disabledWeatherAutomations | ESPAutomation[] (computed) | Disabled weather automations. |
| enabledDaylightAutomations | ESPAutomation[] (computed) | Enabled daylight automations. |
| disabledDaylightAutomations | ESPAutomation[] (computed) | Disabled daylight automations. |
| automationsByNodeId | { [key: string]: ESPAutomation[] } | Automations grouped by node ID. |
| afterSetAutomationListHook | (automations: ESPAutomation[]) => void | Hook function called after setting the automation list. |
| beforeSetAutomationListHook | (automations: ESPAutomation[]) => void | Hook function called before setting the automation list. |
Example Usage
// Get all automations
const allAutomations = automationStore.automationList;
console.log("Total automations:", allAutomations.length);
// Get node automations
const nodeAutomations = automationStore.nodeAutomationList;
console.log("Node automations:", nodeAutomations);
// Get weather automations
const weatherAutomations = automationStore.weatherAutomationList;
console.log("Weather automations:", weatherAutomations);
// Get daylight automations
const daylightAutomations = automationStore.daylightAutomationList;
console.log("Daylight automations:", daylightAutomations);
// Get automation by ID
const automation = automationStore.nodeAutomations["automation123"];
// Get geographic coordinates
const coordinates = automationStore.geoCoordinates;
if (coordinates) {
console.log("Latitude:", coordinates.latitude);
console.log("Longitude:", coordinates.longitude);
}
// Check for pagination
if (automationStore.hasNext) {
await automationStore.fetchNext();
}
// Get enabled automations
const enabled = automationStore.enabledAutomations;
// Get automations for a specific node
const nodeAutomations = automationStore.automationsByNodeId["node123"];
AutomationStore Methods
Sync Automation List
To fetch the list of automations from the cloud and update the store, use the syncAutomationList() method. Automations are automatically organized by event type.
try {
await automationStore.syncAutomationList();
console.log("Automations synced:", automationStore.automationList);
} catch (error) {
console.error("Error syncing automations:", error);
}
Set Automation List
To set the list of automations in the store, organizing them by event type, use the setAutomationList() method.
/*
- automations: Array of automations to set
*/
automationStore.setAutomationList(automations);
Set Automation List with Hooks
To set the automation list with pre- and post-processing hooks, use the setAutomationListWithHooks() method. This function allows developers to inject custom logic before and after setting the automation list.
/*
- automationList: Array of automations to set
*/
automationStore.setAutomationListWithHooks(automationList);
Add Automation
To add a new automation to the store and make it observable, use the addAutomation() method.
/*
- automation: The automation to add
*/
try {
const automation = await userStore.user.addWeatherBasedAutomation({...});
const observableAutomation = automationStore.addAutomation(automation);
console.log("Automation added:", observableAutomation);
} catch (error) {
console.error("Error adding automation:", error);
}
Get Automation by ID
To get an automation by ID from all lists, use the getAutomationById() method.
/*
- automationId: The automation ID
*/
const automation = automationStore.getAutomationById("automation123");
if (automation) {
console.log("Automation found:", automation);
} else {
console.log("Automation not found");
}
Get Automations by Node ID
To get automations for a specific node (only node-based automations), use the getAutomationsByNodeId() method.
/*
- nodeId: The node ID
*/
const nodeAutomations = automationStore.getAutomationsByNodeId("node123");
console.log("Node automations:", nodeAutomations);
Get Weather Automations by Location
To get weather automations for a specific location, use the getWeatherAutomationsByLocation() method.
/*
- location: The location coordinates
*/
const locationAutomations = automationStore.getWeatherAutomationsByLocation({
latitude: 37.7749,
longitude: -122.4194
});
console.log("Location automations:", locationAutomations);
Get Daylight Automations by Location
To get daylight automations for a specific location, use the getDaylightAutomationsByLocation() method.
/*
- location: The location coordinates
*/
const locationAutomations = automationStore.getDaylightAutomationsByLocation({
latitude: 37.7749,
longitude: -122.4194
});
console.log("Location automations:", locationAutomations);
Update Automation
To update an automation in the store, use the updateAutomation() method.
/*
- automationId: The ID of the automation to update
- update: The update data
*/
automationStore.updateAutomation("automation123", {
automationName: "Updated Name"
});
Update Multiple Automations
To update multiple automations at once, use the updateMultipleAutomations() method.
/*
- updates: Array of automation updates
*/
automationStore.updateMultipleAutomations([
{ automationId: "automation1", automationName: "Updated Name 1" },
{ automationId: "automation2", enabled: false }
]);
Delete Automations
To delete automations from the store, use the deleteAutomations() method.
/*
- ids: Array of automation IDs to delete
*/
try {
await automationStore.deleteAutomations(["automation1", "automation2"]);
console.log("Automations deleted successfully");
} catch (error) {
console.error("Error deleting automations:", error);
}
Fetch Next Page
To fetch the next page of automations when pagination is available, use the fetchNext() method.
if (automationStore.hasNext) {
try {
const response = await automationStore.fetchNext();
console.log("Next automations:", response.automations);
} catch (error) {
console.error("Error fetching next page:", error);
}
}
Process Automations Response
To process the automation response and update the store, use the processAutomationsRes() method.
/*
- response: The response from the automation API
*/
const processedResponse = automationStore.processAutomationsRes(response);
Set Geographic Coordinates
To set geo coordinates in the store, use the setGeoCoordinates() method.
/*
- coordinates: The geo coordinates to set
*/
automationStore.setGeoCoordinates({
latitude: 37.7749,
longitude: -122.4194
});
Clear Store
To clear all automation data from the store, use the clear() method.
automationStore.clear();
Add Custom Property
To dynamically add an observable property to the store with getter and setter methods, use the addProperty() method.
/*
- propertyName: Name of the property to add
- initialValue: Initial value for the property
*/
automationStore.addProperty("customData", {});
// Creates: customData, getCustomData(), setCustomData(value)
SDK Methods Available via User Instance
Automation operations can be performed through the ESPRMUser instance. For detailed documentation, refer to the ESPRMUser API Reference.
Automation Methods
getAutomations(params)- Get automations with pagination. See Automation documentationgetAutomationDetail(automationId)- Get automation details. See Automation documentationaddWeatherBasedAutomation(params)- Add weather-based automation. See Automation documentationaddDaylightBasedAutomation(params)- Add daylight-based automation. See Automation documentationsetGeoCoordinates(coordinates)- Set geographic coordinates. See Automation documentationgetGeoCoordinates()- Get geographic coordinates. See Automation documentation
SDK Methods Available via Automation Instance
Each automation in the AutomationStore is an ESPAutomation instance. These methods are available directly on automation objects.
Automation Management Methods
updateName(name)- Update automation nameupdateLocation(location)- Update automation locationenable(enabled)- Enable or disable the automationupdateEvents(events)- Update automation events/triggersupdateActions(actions)- Update automation actionssetRetrigger(retrigger)- Set retrigger behaviorupdate(automationDetails)- Update multiple automation propertiesdelete()- Delete the automation
Accessing Automation Properties
Once you have an automation from the store, you can access its properties:
const automation = automationStore.getAutomationById("automation123");
// Automation properties
console.log("Automation ID:", automation.automationId);
console.log("Automation Name:", automation.automationName);
console.log("Event Type:", automation.eventType);
console.log("Enabled:", automation.enabled);
console.log("Location:", automation.location);
console.log("Events:", automation.events);
console.log("Actions:", automation.actions);
console.log("Node ID:", automation.nodeId); // For node automations
console.log("Retrigger:", automation.retrigger);
Automation Types
The AutomationStore organizes automations by event type:
Node Automations
Automations triggered by node parameter changes. These automations are associated with specific nodes.
const nodeAutomations = automationStore.nodeAutomationList;
const nodeAutomationsForNode = automationStore.getAutomationsByNodeId("node123");
Weather Automations
Automations triggered by weather conditions. These automations require geographic coordinates.
const weatherAutomations = automationStore.weatherAutomationList;
const locationAutomations = automationStore.getWeatherAutomationsByLocation({
latitude: 37.7749,
longitude: -122.4194
});
Daylight Automations
Automations triggered by daylight/sunset conditions. These automations require geographic coordinates.
const daylightAutomations = automationStore.daylightAutomationList;
const locationAutomations = automationStore.getDaylightAutomationsByLocation({
latitude: 37.7749,
longitude: -122.4194
});
Reactive Updates
The AutomationStore is reactive, so you can observe changes:
import { useEffect } from 'react';
useEffect(() => {
console.log("Automation list updated:", automationStore.automationList);
}, [automationStore.automationList]);
useEffect(() => {
console.log("Node automations:", automationStore.nodeAutomationList);
}, [automationStore.nodeAutomationList]);
useEffect(() => {
console.log("Weather automations:", automationStore.weatherAutomationList);
}, [automationStore.weatherAutomationList]);
Automatic Store Synchronization
The AutomationStore automatically synchronizes when automations are created, updated, or deleted through the SDK. Automation operations update the store through interceptors.
Additional Resources
- Automation Triggers Documentation - Overview of automation features
- Automation Documentation - SDK automation methods