Function for Brewfather-API-node in NODE-RED

mail@pastecode.io avatar
unknown
javascript
a year ago
884 B
13
Indexable
Never
// Access the data object directly from the input payload
const data = msg.payload;

// Get the current timestamp
const now = new Date().getTime();

// Find the current fermentation step based on the actualTime property
const currentStep = data.recipe.fermentation.steps.find(
  (step, index, steps) => {
    if (index === steps.length - 1) {
      // If it's the last step, consider it as the current step
      return true;
    } else {
      // Check if the actualTime of the next step is greater than the current timestamp
      return now >= step.actualTime && now < steps[index + 1].actualTime;
    }
  }
);

if (currentStep) {
  // Set the current step's stepTemp as the output payload
  msg.payload = currentStep.stepTemp;
} else {
  // Set an error message as the output payload if no current step is found
  msg.payload = "No current fermentation step found";
}

return msg;