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);
}