Hi PuzzleStar,
Thank you so much for your work. It looks really good and I am eager to adopt it. Need to set up the right environment for it still. In the meantime, maybe my note here could help someone (like me before).
About a month back I started with HA and I was dismayed by the automation interfaces. I found the kettlepod thread early on (totally resonated), and of course your work. But I had barely started. I knew the HA concepts and had tinkered, but I wasnāt ready to go outside the box so soon.
Itās about a month later, and I have some pretty darn complicated automations going with almost pure JS code. I came up with mechanisms that work ok. I think this counts as a healthy stepping stone to adopting your work. For others new to the scene and want to just write JS ⦠this is what I think would have helped me.
My setup:
- HAOS install (anyone new definitely start with that).
- Node-Red and the Node-Red companion
the rest is about how to use Node Red, such that you get close to an environment that FE developers would be ok with. Node Red has a learning curve but not that bad:
The key tips:
Emulate JS Modules (export and import):
- use function nodes like JS modules. Define reusable code and use global.set() in place of export.
- Likewise, use global.get() in place of import to pull code in.
So library code in a function node looks like this:
const { debug } = global.get('core'); // import a core routine
const lib = {
add: (x, y) => {
debug(`i'm adding ${x} and ${y}`);
return x+y;
},
...
};
global.set('lib', lib); // export
return msg; // you need this
Get those dependencies to run in the right order
You need to ensure dependencies run first. Donāt use āSetupā, āon Startā or āOn Stopā in the function node. Have all code defined in the standard āOn Messageā tab. You just set up an inject node to inject once after ā0.1 secondsā (not 0). and tie your dependences together. If your dependency tree gets a little complicated like mine, use join nodes (set mode to manual, and āafter a number of msg partsā set to number of nodes it is listening to). See below for the picture.
Get console.log() working for you
Console.log is available. Debug nodes arenāt great. In HAOS, Node Red runs in a separate docker container but you can totally access the logs for it and tail in a terminal:
- āUse Advanced SSH & Web Terminalā Add On, with āProtection modeā disabled
- SSH to HA ⦠and run ādocker logs -f addon_a0d7b954_noderedā ⦠it tails the console.log.
Like every better environment, wrap console.log so it spits out the date, the module the code is in etc.
Understand the limitations
You can do a lot with just JS code. You have access to the entire state of HA
(global.get(āhomeassistantā)). But listening to state changes, and issuing commands/actions - best to do that with separate nodes. I mostly use just three:
- inject node (interval)
- events: state (listen to state change)
- action node (to make changes)
For all the actions I form in JS code (just an object) and the action node I pass the object to and it sends it to HA. I donāt have to finangle with JSONata or anything like that. Itās a little annoying to have to figure it out these JSON structs, but chatgpt usually gets it right, and you can figure them out from the developer tools in HA. Thereās a pattern to it and you build up your library for all the types of entities you need to control.
For more complicated things:
- expose parameters in dashboards via HA helpers (input_number, input_boolean, schedule etc). Think of HA as your state and UI, and Node Red as automation.
- store internal state in Node Red using global/flow/context. You can persist to disk (āfileā setting, ask chatgpt).
- you can trigger and use all the HA things ⦠AND ⦠you have access to node red stuff too from the Node red community catalogue. I pulled in SunCalc JS code + doing math myself, but still installed āthingzi-logic-timersā for sun timers cause itās so convenient.
Some things I dislike which makes me want to switch away:
- No typescript, no code completion/understanding of code outside the immediate function node. So it is easy to make typos and harder to maintain.
- No version control.
- The editing isnāt bad. I got tab size to 2 spaces (took some searching) and multi-line editing is there (VScode editor under the hood). But itās limited.
- If you open multiple browser windows so you can look at code in one function node while editing another, it can get hairy with those getting out of sync with each other. You have to be careful not to lose changes.
Hope this helps.