Files
HA-NerdFlows-Functions/subflows/user-conversion.js

36 lines
1.0 KiB
JavaScript

// Get the user ID list from global context
const users = global.get("userIDList", "diskCon") || {}
// Initialize variables
let userTrigger = "none"
let statusTxt = ""
// Extract user ID from the message
if (msg.payload?.context?.user_id) {
userTrigger = msg.payload.context.user_id
} else if (msg.data?.context?.user_id) {
userTrigger = msg.data.context.user_id
}
// Validate userTrigger and set status message
if (userTrigger === "none" || !users[userTrigger]) {
statusTxt = "User not found"
node.error(`User ID '${userTrigger}' not found in user list.`)
return null // Stop execution if user is not found
} else {
const user = users[userTrigger]
statusTxt = `User: ${user.firstName}`
msg.user = { ...user } // Avoid mutating the original msg
}
// Create the status message
const statusMsg = {
status: {
fill: "green",
shape: "dot",
text: `${statusTxt} at ${new Date().toLocaleString()}`
}
}
// Send the message and status message to the output nodes
node.send([msg, statusMsg])