Welcome!
@digital-alchemy
is an ergonomic Typescript framework with the goal of providing the easiest text-based automating experience. The tools are straightforward and friendly to use, allowing you to have a working first automation in a few minutes.
Previous experience writing code not required! (it does help)
All of the tools are customized to your specific instance. Know exactly how to call that service without looking at the documentation. Never call fan.turn_on
with a light again!
Links
Changelog is maintained in a discord channel (build #, link to pr with details on what was changed)
Repo | Description | |
---|---|---|
Wiring, configuration, boilerplate utils | ||
Friendly API bindings for home assistant | ||
Entity creation tools (typescript side) | ||
synapse-extension | Entity creation tools (custom component) | |
Support tool for Home Assistant to Typescript type conversions | ||
Advanced tools for building more powerful automations with less code |
Getting started
Home Assistant
2024.4
or higher required
The project has two main starting points depending on your current setup:
- HAOS Based: For those who want to use the Studio Code Server add-on to get the project started, run the dev server, and maintain the code. Also has access to a Code Runner to run a production copy of your code in the background.
- Generic: This details the setup without all the Home Assistant-specific tooling and focuses more on cross-environment support and
docker
/pm2
based production environments.
These pre-built projects are intended as starting points. There isn’t any complex requirements under the hood though, so you’re able to easily customize to your needs.
Writing logic
All code using @digital-alchemy
follows the same basic format. You gain access to the various library tools by importing TServiceParams
, then write your logic inside a service function.
Your services get wired together at a central point (example, docs), allowing you to declare everything that goes into your project and the required libraries. Adding new libraries adds new tools for your service to utilize, and your own services can be wired together to efficiently lay out logic.
import { TServiceParams } from "@digital-alchemy/core";
export function ExampleService({ hass, logger, ...etc }: TServiceParams) {
// logic goes here
}
The hass
property is a general purpose bag of tools for interacting with your setup. It forms the backbone of any automation setup with:
- Service calling interfaces customized to your integrations
- Entity references that allow for a variety of convenient interactions
- Access to all the internal tools to build your own modules
Do things the easiest way
A big focus of the framework is providing you the tools to express yourself in the way that is easiest in the moment. For an example call to light.turn_on
Via service call:
// a quick service call
hass.call.light.turn_on({ entity_id: "light.example", brightness: 255 });
// this time with some logic
hass.call.light.turn_on({ entity_id: "light.example", brightness: isDaytime? 255 : 128 });
Via entity reference:
// create reference
const mainKitchenLight = hass.refBy.id("light.kitchen_light_1")
// issue call
mainKitchenLight.turn_on({ brightness: isDaytime? 255 : 125 });
How custom is this?
All of the tools are powered by the same APIs that run the Developer Tools screen of your setup.
The type-writer
script will gather all the useful details from your setup, allowing the details to be updated at any time.
- entity attributes are preserved
- all integration services available
- helpful text provided by integration devs preserved as tsdoc
- suggestions are
supported_features
aware
Want to spend an emergency notification to a specific device? Easy!
hass.call.notify.mobile_app_air_plant({
data: {
color: "#ff0000",
group: "High Priority",
importance: "max",
},
message: "Leak detected under kitchen sink",
title: "🚰🌊 Leak detected",
});
The notification: Imgur: The magic of the Internet
Entity references
For building logic, entity references really are the star of the show. They expose a variety of useful features for expressing your logic:
- call related services
- access current & previous state
- receive update events
- and more! (no really)
In a simple event → response example:
// create references
const isHome = hass.refBy.id("binary_sensor.is_home");
const entryLight = hass.refBy.id("light.living_room_light_6");
// watch for updates
isHome.onUpdate((new_state, old_state) => {
logger.debug(`changed state from %s to %s`, new_state.state, old_state.state);
// gate logic to only return home updates
if (new_state.state !== "on" || old_state.state !== "off") {
return;
}
// put together some logic
const hour = new Date().getHours(); // 0-23
const isDaytime = hour > 8 && hour < 19;
// call services
hass.call.notify.send({ message: "welcome home!" });
entryLight.turn_on({ brightness: isDaytime ? 255 : 128 });
});
Getting more practical
Using just the tools provided by hass
, and some standard javascript code, you can build very complex systems. That’s only the start of the tools provided by the project though. As part of the the quickstart project, there is an extended example.
It demonstrates a workflow where some helper entities are created via the synapse library. These put together to coordinate the scene of a room based on the time of day and the presence of guests. It also includes example of the scheduler in use, as well as tests against time and solar position being made.
Conclusions
@digital-alchemy
is a powerful modern Typescript framework capable of creating production applications. It has a fully featured set of plug in modules for a variety of uses, with the ability to easily export your own for others.
If you’re looking for a practical tool that is friendly to whatever crazy ideas you want to throw at it, and more than capable of running for long periods without being touched, look no further.
Digital Alchemy is a passion project that is is entirely free, open-source, and actively maintained by yours truly. For a perspective from one of the early testers:
Migrating my HomeAssistant automations from NodeRED to Digital-Alchemy
Question for those who make it this far:
What is a workflow you would like to see a demo of?
I am setting up an example project to showcase various ways to use the library and provide some inspiration for building automations.