// Paste into the "Clear zigbee edge" Function node (after 80 ms delay).
// Clears zigbee_swN in nvlInState so the PLC sees one pulse. Supports one room or many (switchBindings).
// msg.zigbeeClear can be: { room, key } or [ { room, key }, ... ]

if (msg.zigbeeClear) {
  const state = flow.get('nvlInState') || { rooms: {}, boiler: {} };
  const list = Array.isArray(msg.zigbeeClear) ? msg.zigbeeClear : [msg.zigbeeClear];
  for (const item of list) {
    const r = state.rooms[item.room];
    if (r && item.key) r[item.key] = false;
  }
  flow.set('nvlInState', state);
}
msg.payload = { buildAndSend: true };
return msg;
