Code
Category: Action
Runs custom JavaScript to transform data, perform calculations, or implement logic that doesn't fit into the other node types. Your code receives an input object with access to upstream node outputs, trigger data, variables, and device state.
When to use
- Transform API response data into a format your devices need
- Perform calculations (unit conversion, averages, thresholds)
- Implement complex conditional logic beyond what the IF node supports
- Parse and restructure data between nodes
Configuration
- Code — JavaScript source code. Your code receives an
inputobject and shouldreturna value. - Timeout — Maximum execution time in milliseconds (default: 5000ms)
The input object
Your code has access to:
javascript
input.trigger // Trigger data (to_value, from_value, etc.)
input.variables // Automation variables
input.nodes // Upstream node outputs: input.nodes.<id>.data.<field>
input.states(id, t) // Query live device state: input.states('acc-1', 'brightness')Output data
Whatever your code returns becomes the node output:
- If you return an object:
{{ nodes['<id>'].data.<field> }} - If you return a primitive:
{{ nodes['<id>'].data.result }}
Examples
Convert Celsius to Fahrenheit:
javascript
const tempC = input.nodes.http1.data.body.temperature;
return { fahrenheit: tempC * 1.8 + 32 };Calculate average of multiple sensors:
javascript
const t1 = input.states('sensor-1', 'temperature');
const t2 = input.states('sensor-2', 'temperature');
const t3 = input.states('sensor-3', 'temperature');
return { average: (t1 + t2 + t3) / 3 };Conditional logic:
javascript
const hour = new Date().getHours();
const brightness = hour < 6 ? 10 : hour < 9 ? 60 : 100;
return { brightness, timeOfDay: hour < 12 ? 'morning' : 'afternoon' };Tips
- Code runs in a sandboxed environment — no access to DOM,
fetch, or browser APIs - Use
input.states()to read the current value of any device, not just the trigger - Keep code simple — for complex multi-step logic, chain multiple Code nodes
- If your code throws an error, the node fails — use try/catch or
onError: continue - The timeout prevents infinite loops — increase it for genuinely slow calculations